Results 1 to 8 of 8
- 12-27-2011, 10:08 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
Using Iterator command to iterate through an ArrayList
Hi folks!
I'm new to Java so I'm suffering noob problems :(. I hope you guys can help me out a bit.
I have created an ArrayList containing several objects. Now I want to benefit from the Iterator command. I want to display the very first object (and delete it right afterwards). But all I was able to do was to display the whole list.
For instance: I have 35 (Integer) objects in my ArrayList. Now I don't want to display the whole list. I just want to select the first object from the list and display it. And after that, I want to delete it immediately so that I only have 34 objects left.
Can someone help me out please?
Greetings, Alpa
- 12-27-2011, 10:10 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Using Iterator command to iterate through an ArrayList
get(index) and remove(index) ?? Read the API Doc : List (Java Platform SE 6)
- 12-27-2011, 10:20 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
Re: Using Iterator command to iterate through an ArrayList
I did find the get(index) command. But I don't really get how to integrate it into the following code:
[JAVA]
Iterator iterator = VARIABLE.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
[/JAVA]
- 12-27-2011, 10:23 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Using Iterator command to iterate through an ArrayList
You don`t need the iterator if you want only one object ?! the iterator is as the name implies, for iterating :D
call VARIABLE.get(0)
Or for your code:
while(iterator.hasNext()){
System.out.println(iterator.next());break;}
but thats ugly in my opinion :D
- 12-27-2011, 11:49 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
Re: Using Iterator command to iterate through an ArrayList
Thanks alot. I don't really get the "get" command so I used the while loop instead which is working perfectly.
I really don't want to bother you but I have another question: How can I combine an if-else statement or a switch-case statement with the displayed object. For instance if I have the number "5" as a result, I want the sentence "This is number FIVE" being printed on the screen. But if I use the following codes:
if(iterator.next() == 5){ System.out.println("This is number FIVE"); } else { System.out.println("This is NOT number FIVE"); }
It's not working. I guess the problem's the condition, iterator.next() == 5. More precisely the iterator.next(). But what do I have to write instead?
- 12-28-2011, 01:31 AM #6
Re: Using Iterator command to iterate through an ArrayList
What kind of data does the next() method return? Your code compares what it returns to an int value.if(iterator.next() == 5)
Does that make sense?
Add a println to show what value is returned by the next() method to see if you are getting any with a value of 5
- 12-28-2011, 07:12 AM #7
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Using Iterator command to iterate through an ArrayList
The problem is, that you dont use a generic iterator, so next() will be for the compiler of type object. no matter whats really in it.
Java Code:Iterator<Integer> iterator = VARIABLE.iterator(); while (iterator.hasNext()) { if (iterator.next() == 5) { //or better use the equals method: if (iterator.next().equals(5)) System.out.println("This is number FIVE"); } else { System.out.println("This is NOT number FIVE"); } }
- 12-28-2011, 10:25 AM #8
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
DIFFERENCE BETWEEN ITERATOR AND iterator( )
By mark clarke in forum New To JavaReplies: 1Last Post: 12-07-2011, 09:41 AM -
How to iterate over objects in a class?
By Neilos in forum New To JavaReplies: 10Last Post: 08-18-2011, 08:02 PM -
iterate through a HashMap ?
By aneuryzma in forum New To JavaReplies: 2Last Post: 03-27-2011, 03:26 PM -
How to iterate through two Arrays?
By aRTx in forum Advanced JavaReplies: 3Last Post: 05-18-2010, 07:48 AM -
Iterator as return and argument or Iterator-String(not visible in the test file)
By Aldarius in forum New To JavaReplies: 0Last Post: 05-18-2010, 12:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks