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
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
line 28  - System.out.println(l);
will always be executed and will print the contents of the list.

Code is working as expected. What is the problem?
CPColinSenior Java ArchitectCommented:
Iterator.remove() removes the last element that was returned by Iterator.next(). 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. Line 28 prints the contents of the list, so it prints even numbers only.
gudii9Author Commented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mccarlIT Business Systems Analyst / Software DeveloperCommented:
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"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
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?
CPColinSenior Java ArchitectCommented:
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.
gudii9Author Commented:
Iterator itr = l.iterator();

For arraylist is above is only way to create Iterator object? Or is there is any other way?
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Yes, it's the only way
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.