gudii9
asked on
iterator example remove
Hi,
when i ran below iterator example
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
[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
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);
}
}
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);
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
ASKER
removes from reference 'itr' right not from reference 'l'
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