When a for loop is being controlled by the size of an ArrayList
(...; i < someArrayList.size(); ...)
but each iteration of the loop is adding an element to the list, how does the loop then recognize that the size is changing? I would think that once the code is sent to the compiler, it would see the size once and then do it until it reaches the original size, but that's clearly not the case. As the size increases, so does the amount of times the loop runs. I would think it wouldn't, so clearly I don't understand how the computer reads this. I'm just wondering how the system reads something like this.
Also, is there some way to make a loop only run the amount of times of the original list size (again element added each iteration)? Assuming you don't know the size beforehand so you can't put in some constant number.
At the beginning of each iteration, the middle expression is evaluated. If it is true, the loop continues. If it is false, the loop breaks. So it will re-evaluate the size every iteration.
To make it loop the number of times of the original size of the list, do something like this:
int size = someArrayList.size();
for (i = 0 ; i < size ; i++) { ... }
Or if you want to get fancy:
for (i = 0, size = someArrayList.size() ; i < size ; i++) { ... }
In the implementation of add() method in ArrayList, a member variable which stores the size of the arraylist is updated and size() represents a getter for this member variable , so on each iteration this size is called so you get the updated version of the size.
The .size() is called for each iteration. That is why looping over iterators works, if you just evaluated .hasNext() only once the loop would never end. To only iterate over the list the number of times as the original size you could do something like this:
final int originalSize = list.size();
for (int i = 0; i < originalSize; i++) {
...
}
I am trying to sort a LinkedList in Java. I need to go through mylist from back to front. The elements in the list are objects from my class CustomElement. If they match a certain pattern I want to put them up front.
My problem is that if I detect that the element in my list with index 5 for example matches my pattern and I move it to index 0, the previous element with index 4 has index 5 now, right? That is why I want the for loop to check the element with index 5 again: i++. But that's causing an infinite loop, whreas the method's working fine without i++, but not the way that I want it, because it's skipping the element with index 4 (now 5).
Is it gernerally possible to raise the variable i inside the for loop? And if yes, what am I doing wrong.
for (int i = mylist.size() - 1; i >= 0; i--) {
if (mylist.get(i) matches a certain pattern) {
CustomElement helper = mylist.get(i);
mylist.remove(i);
mylist.add(0, helper);
i++;
}
}
Yes, it is possible to modify i inside your for loop, if it weren't possible, you wouldn't be getting this infinite loop.
What must be happening, is that if (mylist.get(i) matches a certain pattern) continues to be true after a certain point, and you never get to a point where i >= 0 is not true.
So, if myList.get(0) matches your pattern, you'll just put it back at index 0, and keep checking it forever.
It is, but in your case using get(i) for a linked list will give quadratic performance.
If you don't mind your "matching" items being reversed in order then you'd be better creating a new list:
final LinkedList<CustomElement> newList = new LinkedList<> ();
for (final CustomElement e: myList)
{
if (e matches your pattern) { newList.addFirst (e); }
else { newList.addLast (e); }
}
myList = newList;
Then all problems with index variables disappear...
(You could also achieve linear performance whilst modifying the existing list, but it's a little more complicated.)
I'm receiving two object's List and I should compare all the objects...
For this I've been using the next loop:
for (int i=0;it2.hasNext();i++ ) {
objetoDb4o=it2.next();
tratdb4o=(Tratam)objetoDb4o;
for (int j=0;it.hasNext();j++ ) {
objetoJson = it.next();
tratJson = (Tratam)objetoJson;
}
}
it and it2 are iterators.. The values of size are correct (4 and 4)
But the loop doesn't work.. the first for iterates only one time, and the second for iterates three times.
I can not find the mistake! Can you help me please??
You need to reset the iterator it before you enter the inner loop, otherwise you'll go through the second list only once during the first iteration of the outer loop; the remaining iterations will stop at it.hasNext(), skipping the inner loop altogether.
I got a weird problem.
I thought this would cost me few minutes, but I am struggling for few hours now...
Here is what I got:
for (int i = 0; i < size; i++){
if (data.get(i).getCaption().contains("_Hardi")){
data.remove(i);
}
}
The data is the ArrayList.
In the ArrayList I got some strings (total 14 or so), and 9 of them, got the name _Hardi in it.
And with the code above I want to remove them.
If I replace data.remove(i); with a System.out.println then it prints out something 9 times, what is good, because _Hardi is in the ArrayList 9 times.
But when I use data.remove(i); then it doesn't remove all 9, but only a few.
I did some tests and I also saw this:
When I rename the Strings to:
Hardi1
Hardi2
Hardi3
Hardi4
Hardi5
Hardi6
Then it removes only the on-even numbers (1, 3, 5 and so on).
He is skipping 1 all the time, but can't figure out why.
How to fix this? Or maybe another way to remove them?
The Problem here is you are iterating from 0 to size and inside the loop you are deleting items. Deleting the items will reduce the size of the list which will fail when you try to access the indexes which are greater than the effective size(the size after the deleted items).
There are two approaches to do this.
Delete using iterator if you do not want to deal with index.
for (Iterator<Object> it = data.iterator(); it.hasNext();) {
if (it.next().getCaption().contains("_Hardi")) {
it.remove();
}
}
Else, delete from the end.
for (int i = size-1; i >= 0; i--){
if (data.get(i).getCaption().contains("_Hardi")){
data.remove(i);
}
}
You shouldn't remove items from a List while you iterate over it. Instead, use Iterator.remove() like:
for (Iterator<Object> it = list.iterator(); it.hasNext();) {
if ( condition is true ) {
it.remove();
}
}
Every time you remove an item, you are changing the index of the one in front of it (so when you delete list[1], list[2] becomes list[1], hence the skip.
Here's a really easy way around it: (count down instead of up)
for(int i = list.size() - 1; i>=0; i--)
{
if(condition...)
list.remove(i);
}
Its because when you remove an element from a list, the list's elements move up. So if you remove first element ie at index 0 the element at index 1 will be shifted to index 0 but your loop counter will keep increasing in every iteration. so instead you of getting the updated 0th index element you get 1st index element. So just decrease the counter by one everytime you remove an element from your list.
You can use the below code to make it work fine :
for (int i = 0; i < data.size(); i++){
if (data.get(i).getCaption().contains("_Hardi")){
data.remove(i);
i--;
}
}
It makes perfect sense if you think it through. Say you have a list [A, B, C]. The first pass through the loop, i == 0. You see element A and then remove it, so the list is now [B, C], with element 0 being B. Now you increment i at the end of the loop, so you're looking at list[1] which is C.
One solution is to decrement i whenever you remove an item, so that it "canceles out" the subsequent increment. A better solution, as matt b points out above, is to use an Iterator<T> which has a built-in remove() function.
Speaking generally, it's a good idea, when facing a problem like this, to bring out a piece of paper and pretend you're the computer -- go through each step of the loop, writing down all of the variables as you go. That would have made the "skipping" clear.
I don't understand why this solution is the best for most of the people.
for (Iterator<Object> it = data.iterator(); it.hasNext();) {
if (it.next().getCaption().contains("_Hardi")) {
it.remove();
}
}
Third argument is empty, because have been moved to next line. Moreover it.next() not only increment loop's variable but also is using to get data. For me use for loop is misleading. Why you don't using while?
Iterator<Object> it = data.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj.getCaption().contains("_Hardi")) {
it.remove();
}
}
Because your index isn't good anymore once you delete a value
Moreover you won't be able to go to size since if you remove one element, the size as changed.
You may use an iterator to achieve that.
for (Iterator<Object> it = data.iterator(); it.hasNext();) {
if ( it.getCaption().contains("_Hardi")) {
it.remove(); // performance is low O(n)
}
}
If your remove operation is required much on list. Its better you use LinkedList which gives better performance Big O(1) (roughly).
Where in ArrayList performance is O(n) (roughly) . So impact is very high on remove operation.
It is late but it might work for someone.
Iterator<YourObject> itr = yourList.iterator();
// remove the objects from list
while (itr.hasNext())
{
YourObject object = itr.next();
if (Your Statement) // id == 0
{
itr.remove();
}
}
In addition to the existing answers, you can use a regular while loop with a conditional increment:
int i = 0;
while (i < data.size()) {
if (data.get(i).getCaption().contains("_Hardi"))
data.remove(i);
else i++;
}
Note that data.size() must be called every time in the loop condition, otherwise you'll end up with an IndexOutOfBoundsException, since every item removed alters your list's original size.
This happens because by deleting the elements you modify the index of an ArrayList.
import java.util.ArrayList;
public class IteratorSample {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(1);
al.add(2);
al.add(3);
al.add(4);
System.out.println("before removal!!");
displayList(al);
for(int i = al.size()-1; i >= 0; i--){
if(al.get(i)==4){
al.remove(i);
}
}
System.out.println("after removal!!");
displayList(al);
}
private static void displayList(ArrayList<Integer> al) {
for(int a:al){
System.out.println(a);
}
}
}
output:
before removal!!
1
2
3
4
after removal!!
1
2
3
There is an easier way to solve this problem without creating a new iterator object. Here is the concept. Suppose your arrayList contains a list of names:
names = [James, Marshall, Susie, Audrey, Matt, Carl];
To remove everything from Susie forward, simply get the index of Susie and assign it to a new variable:
int location = names.indexOf(Susie);//index equals 2
Now that you have the index, tell java to count the number of times you want to remove values from the arrayList:
for (int i = 0; i < 3; i++) { //remove Susie through Carl
names.remove(names.get(location));//remove the value at index 2
}
Every time the loop value runs, the arrayList is reduced in length. Since you have set an index value and are counting the number of times to remove values, you're all set. Here is an example of output after each pass through:
[2]
names = [James, Marshall, Susie, Audrey, Matt, Carl];//first pass to get index and i = 0
[2]
names = [James, Marshall, Audrey, Matt, Carl];//after first pass arrayList decreased and Audrey is now at index 2 and i = 1
[2]
names = [James, Marshall, Matt, Carl];//Matt is now at index 2 and i = 2
[2]
names = [James, Marshall, Carl];//Carl is now at index 3 and i = 3
names = [James, Marshall,]; //for loop ends
Here is a snippet of what your final method may look like:
public void remove_user(String name) {
int location = names.indexOf(name); //assign the int value of name to location
if (names.remove(name)==true) {
for (int i = 0; i < 7; i++) {
names.remove(names.get(location));
}//end if
print(name + " is no longer in the Group.");
}//end method
This is a common problem while using Arraylists and it happens due to the fact that the length (size) of an Arraylist can change. While deleting, the size changes too; so after the first iteration, your code goes haywire. Best advice is either to use Iterator or to loop from the back, I'll recommend the backword loop though because I think it's less complex and it still works fine with numerous elements:
//Let's decrement!
for(int i = size-1; i >= 0; i--){
if (data.get(i).getCaption().contains("_Hardi")){
data.remove(i);
}
}
Still your old code, only looped differently!
I hope this helps...
Merry coding!!!
Currently I am writing a program that must iterate through and arraylist inside of a for loop which looks like this.
List<Integer> currentLevel = new ArrayList<Integer>();
List<Integer> nextLevel = new ArrayList<Integer>();
Iterator<Integer> it = currentLevel.iterator();
currentLevel.add(1);
for(x=0;x<20;x++){
while(it.hasNext()){
int element = it.next();
nextLevel.add(element*2);
if(element%6 == 4){
nextLevel.add((element-1)/3);
}
}
currentLevel.clear();
currentLevel.addAll(nextLevel);
nextLevel.clear();
}
With this code it seams that it only goes through the while loop once. Is this because after the first loop it only adds one number to the nextLevel array and the currentLevel array has the same amount of indexes as before? Or is it something that I am leaving out? If I try to add additional elements to the nextLevel array after the while loop it gives me an error at the line
int element = it.next();
You're entering the for loop several times, but can only enter the while loop on the first pass through the for loop.
The iterator, it is defined outside the for loop and only assigned once.
The first time through the for loop you enter a while loop:
while(it.hasNext()){
int element = it.next();
...
}
which completely exhausts the iterator. Every subsequent time through the for loop, it.hasNext() is false, so the while loop does nothing.
You're also trying to re-use nextLevel and currentLevel, but those are only assigned once. So currentLevel will only contain the last set of elements added to nextLevel.
If I try to add additional elements to the nextLevel array after the while loop it gives me an error at the line
You are probably getting a concurrent modification exception. You can not use an iterator for a list after you modify it. https://stackoverflow.com/a/1496206/20394 explains how to deal with these problems.