I have two dimensional arrayList, and I want to replace the that is set in the program. The problem is, if i replace the value of arrayList by "ONE", it will work, if I replace it by another value bigger than one, the program enters into a loop. I am using the following syntax:
arrayList.get(index).set(index2,VALUE)
Basically this shouldn't be happening, cause I am not changing anything within the loops, or?
My Questions is WHY? and How to fix it?
the code that is (i assume) producing the problems is:
if(mark.get(index1).get(index2) == 1 && mark.get(i-1).get(j) != 1){
// Replace the value
mark.get(i-1).set(j,1);
flag = true;
}
EDIT: I removed my code, cause it gave the impression I wanted help with the code, i had posted it so that you know what I was talking about. thanks
Got what the problem was! All the initials indexes had a value of ZERO, and I had a condition that was checking if it was not ONE(assuming it will be zero anyway). if i changed it to any other value than ONE then in the next repetitions the condition would have been voided.
Related
I try to remove a value from a map when unchecked but for some reason, it gives me an IndexOutOfBoundsException
itemView.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
item.isChecked = isChecked
if(isChecked){
map.put(position,true)
} else {
map.removeAt(position)
}
}
Here I have 2 checkboxes, when I check both is ok. When I uncheck the first one is ok, but when I try to uncheck also the second one it crashes with this error
java.lang.ArrayIndexOutOfBoundsException: src.length=11 srcPos=2 dst.length=11 dstPos=1 length=-1
The problem is at map.removeAt(position) but I don't know why it is failing because that position is an element from the array that actually exists.
I'm using a SparseBooleanArray.
The problem is not the position of the array as I said, because this array never changes (I never delete any items on it, just removing values on the map that contains this mapped booleans values)
So the problem was in removeAt(position)
Example
I have a Map array with 2 values
0 - true
1 - true
If I do map.RemoveAt(0) now I have
1 - true (actually in position 0 right now)
but now if I try to do again map.removeAt(1) there is not element in position 1 so that is why is outbounded
I have solved it by removing by key in the map
map.remove(position,true)
So this will remove that 1 - true value and just the values with true as the value of that map
Your code doesn't show where position is being set, but if I had to guess, you probably need to be doing position - 1
Remember that your arrays are 0 indexed!
I saw the answer that you posted. Yes, you have figured that out correctly. You were getting the IndexOutOfBoundException because the position that you are looking for was already removed.
However, you do not have to use the key-value pair to remove that element from your map. It reduces the scalability of your Map object as somewhere down the road, the value might be changed and if you are not aware of that change, you might run into a similar problem, where the data will not be removed. Let us take the following example.
0 - true
1 - true
Now somewhere you have changed the value for key 1 to false. Hence when you will call the map.remove(position, true) this will not find anything to remove.
I would recommend using the remove function with the key only. The following should suffice.
if (map.containsKey(position)) map.remove(position);
I hope that helps!
This question already has answers here:
for loop VS while loop in programming languages, c++/java?
(9 answers)
Closed 9 years ago.
Some iteration code is written like this
while(true) for(int i = 0 ; i<=4;++i)
{
{
System.out.println(i); System.out.println(i);
if(i==4)
break;
}
}
both have the same logic inside it take it as assumption. So which one will be faster, better to use in code and what are the implication of using any one of it??
The first one is an endless loop :) (depending on the i before the loop) The second one does (almost) what you expect. Go grab a good manual about your programming language, it will explain the difference.
On another level: Loop optimizations are much better suited to for loops (AFAIK) so a "clever" compiler might generate better code for the "for" loop. http://en.wikipedia.org/wiki/Loop_optimization
They are totally different, but if their behavior was the same, never use break; in such situations.
The second bucle is more clear, when you have to take into consideration performance, you don't have to do it in retreat of clean code.
The second bucle is self-documented while the first you need to add a reason to add the break in order to be understandable.
to end the while loop on the left you have to incriment i
EDIT : Assuming i is less that 4 when the loop starts
For that purpose, use a for one, since you need the value of i. It is more understanding for maintenance. Anyway, the compiler will optimize both codes and runtime may be the same.
Considering your while is not an infinite loop there is no difference as far as performance is concerned. However, I personally prefer for loop as it is cleaner. Consider this:
int i = 0;
while (true) {
doSomething(i++);
if (i == 4)
break;
}
which looks cleaner with
for (int i = 0; i <= 4; i++)
doSomething(i);
Im using for whenever I know the number of itterations in advance.
If you know how many time you want to do some operation you have to use for loop otherwise use while loop.
For example:
If you have some array or arraylist then suppose you want to add all
elemnts of it then you should use for loop because you know size of
arraylist by size() function and know the size of array by length
attribute.
If you have resultset and you want to retrieve all rows from it then
you should use while loop because you don't know how many element in
resultset object.
Neglecting the typo of your question,Assuming that you want to know which one i choose when Both loops are essentially doing the same thing.
The answer is you can go for any one.
But it's better to use for loop if you know how many times you want to run a loop as you can set the condition during initialization. And in future also it will be easy to change it if you want to extend the iteration.
You can prefer while if you are not sure for how much iteration you want. In your case i will go with for loop
Note: the answer is opinion based.
i have 3 textboxes. One textbox contains the some integer, eg:2 What i want to do is i want to iterate the loop 2 times.. in every iteration i want to print the value in textboxes. I am clearing the textbox before going to the next iteration. After 2 iterations i want to make the textboxes editable false. But the problem is the loop iterates 2 times but the value in textbox is printed only for the 1st iteration. I wrote the following code
int i=1;
do
{
System.out.println(ext_people_name.getText());
System.out.println(ext_contact_num.getText());
System.out.println(i);
ext_people_name.setText("");
ext_contact_num.setText("");
i++;
if(i>nop1)
{
ext_people_name.setEditable(false);
ext_contact_num.setEditable(false);
break;
}
}while(i<=nop1);
The output i get right now is
neha
8798
1
2
Can anyone tell any alternative to this or please help
I am clearing the textbox before going to the next iteration
There, you solved your own problem!
Its better to use for loop instead of do/while for your code. Refactor your code like this:
for (int i=1; i<=nop1; i++) {
System.out.println(ext_people_name.getText());
System.out.println(ext_contact_num.getText());
System.out.println(i);
ext_people_name.setText("");
ext_contact_num.setText("");
};
ext_people_name.setEditable(false);
ext_contact_num.setEditable(false);
You gave the answer yourself - you CLEAR the textbox before the next iteration. So, it does not contain a value, so it won't print a value.
What you need to do, is to redesign what you are doing and break it into smaller steps. Instead of trying to use one loop to do everything, break it down into smaller loops.
I also do not quite understand what you want to do precisely. Why is the textboxes cleared before the next iteration?
If you only want to display the textbox values twice, and then clear them and make them uneditable, do that in separate steps.
Alternatively, edit your original question and add some more context that we can understand it better.
You are clearing the value in the textbox after each iteration, this make the value empty and prints nothing
System.out.println("some "+ext_people_name.getText());
System.out.println("some "+ext_contact_num.getText());
use above code that, you can come across that you are overwirting the result after iteration
This question already has answers here:
Java - IndexOutOfBoundsException: Index: 1, Size: 0
(1 answer)
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed last year.
I've got a very annoying problem with some code throwing an IndexOutOfBoundsException and I really cannot understand why. The logcat points to the "addTimetableItem" of the following code which ill explain more on:
if(sortedFridayTimes.size()>0){
insertDay("Friday");
for(int i=1; i<sortedFridayTimes.size()+1;i++){
addTimetableItem(sortedFridayTimes.get(i));
}
}
"sortedFridayTimes" is an ArrayList containing my own "Timetable Entry" objects which I have sorted into order already. First the size is checked to see if there are any objects and if there is then "insertDay" runs which creates a new textview for a title and adds it to the layout (This works fine..).
Inside the for loop the idea is to then add all the objects from the arraylist into the layout. Now I know that the "addTimetableItem" code works as ive tested it already, but my problem is that i cant seem to get the last object out of the arraylist. If I declare the for loop to only run for
"i<sortedFridayTimes.size()"
then the program runs fine but I don't get the last entry in the arraylist which I know exists because I've debugged and watched my variables. On adding the "+1" as shown above I now get the IndexOutOfBoundsException and I really don't know why. As I've said, I've debugged and I know that an entry exists in the arraylist where I'm trying to point to, but it just crashes. I can provide more code if needs be, but does anyone have any ideas please?
You should accept #Tim's or #Graham's answer, this is just an addendum. They're correct about your size()+1 going past the end of the array.
If you're having difficulty using indexes to properly get everything out of the list, you can also try using a for-each loop (depending on the version of the Android SDK you're using). I'm assuming sortedFridayTimes is a list of class TimetableItem since you don't specify.
So this:
if(sortedFridayTimes.size()>0){
insertDay("Friday");
for(int i=1; i<sortedFridayTimes.size()+1;i++){
addTimetableItem(sortedFridayTimes.get(i));
}
}
Becomes this:
if(!sortedFridayTimes.isEmtpy()){
insertDay("Friday");
for(TimetimeItem item : sortedFridayTimes){
addTimetableItem(item);
}
}
A little cleaner if you don't actually need to use i anywhere.
i<sortedFridayTimes.size()+1
You are looping past the last element in the array. Why the +1?
If there are N elements in the array, then the elements are from indexes 0 through to N-1.
So it should be:
for(int i=0; i<sortedFridayTimes.size(); i++) {
The last loop in your for loop runs:
sortedFridayTimes.get(sortedFridayTimes.size())
This will always be out of bounds, because the elements are zero indexed.
For example, if the array size is "5", then you cannot access index "5", because the 5 elements in the array are 0,1,2,3,4.
I am using an if statement as shown below,
if(sign.size()==0)
Here sign is of the type ArrayList<Character>
I am trying to add a char to the ArrayList
But its not working. Is there anything wrong with my if statement?
I also tried the same with an ArrayList<doubler>, this time I could get into the if statement.
Is there anything wrong with the if statement?
I am having very difficult time understanding what you are saying, but it sounds like you are trying to figure out how to increase the list capacity. You do not need to manually do this. Simply add items using add() method and the list will re-size itself as appropriate.
Try if(sign.isEmpty()) instead. Also make sure you you're using good code style with {'s where needed. ArrayLists will adjust their own size to accommodate what you put in, so I'm not sure you even need this check.
You commented on an answer and said,
i want to get in the if only if i have 1 char in the array... but i does not get in to the if.... way if i have only one index
If you want to enter the IF only when there is one char, then you'd want
IF(sign.size() == 1){
...some code
}
Otherwise, if you want to enter the IF when size is 0, use isEmpty.
As you said
I want to get in the if only if I have
1 char in the array... but it does not
get in to the if.... what if i have
only one index?
If you are trying to enter the if statement only when there is only one char in the ArrayList then you can use
if(sign.size() == 1){
//your code
}
The condition will only be true when the size of the ArrayList is equal to 1.