Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

iterator example remove

Hi,
when i ran below iterator example
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class IteratorEx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ArrayList l = new ArrayList();
		for (int i = 0; i <= 10; i++) {

			l.add(i);
		}
		System.out.println(l);

		Iterator itr = l.iterator();

		while (itr.hasNext()) {

			Integer I = (Integer) itr.next();
			if ((I % 2 == 0))
				System.out.println(I);
			// else
			// itr.remove();
		}
		System.out.println(l);
	}
}

Open in new window


i got below output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
2
4
6
8
10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


when i uncommented else block as below
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class IteratorEx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ArrayList l = new ArrayList();
		for (int i = 0; i <= 10; i++) {

			l.add(i);
		}
		System.out.println(l);

		Iterator itr = l.iterator();

		while (itr.hasNext()) {

			Integer I = (Integer) itr.next();
			if ((I % 2 == 0))
				System.out.println(I);
			   else
			   itr.remove();
		}
		System.out.println(l);
	}
}

Open in new window

when i ran i got below output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
2
4
6
8
10
[0, 2, 4, 6, 8, 10]


my question is in line 26 when i said
itr.remove()
how line 28 was notified for below line to print only even numbers.
System.out.println(l); //[0, 2, 4, 6, 8, 10]
I mean l was never referred after i removed odd numbers from itr right?

please advise
SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

Since your code is calling remove() for every element that is not an even number, the list only has even numbers left in it after the while loop completes.

removes from reference 'itr' right not from reference 'l'
Line 28 prints the contents of the list, so it prints even numbers only.

as it only removes from itr not from l i though when we print l i thought it should print all numbers from 0 to 10 including even and add.

please advise
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

The iterator "itr" is like a live view of the underlying list "l" so no, whatever changes that you do via "itr" WILL affect the underlying list "l"

that is interesting and i wonder how it does behind the scenes?
It depends on the type of collection. For an ArrayList, it likely keeps track of the current index and increments it every time you call next(). You can view the source code for the standard Java library if you want more details.
Avatar of gudii9

ASKER

Iterator itr = l.iterator();

For arraylist is above is only way to create Iterator object? Or is there is any other way?
Yes, it's the only way