Results 1 to 8 of 8
  1. #1
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default 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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Using Iterator command to iterate through an ArrayList

    get(index) and remove(index) ?? Read the API Doc : List (Java Platform SE 6)

  3. #3
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default 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]

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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

  5. #5
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default 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?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Using Iterator command to iterate through an ArrayList

    if(iterator.next() == 5)
    What kind of data does the next() method return? Your code compares what it returns to an int value.
    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

  7. #7
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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");
    			}
    		}

  8. #8
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: Using Iterator command to iterate through an ArrayList

    Quote Originally Posted by Norm View Post
    What kind of data does the next() method return? Your code compares what it returns to an int value.
    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
    next() is giving me a random number between 0 and 35. Now I want to check how often I receive number 5 as answer. And if I receive it, I want it to display the mentioned sentence.

Similar Threads

  1. DIFFERENCE BETWEEN ITERATOR AND iterator( )
    By mark clarke in forum New To Java
    Replies: 1
    Last Post: 12-07-2011, 10:41 AM
  2. How to iterate over objects in a class?
    By Neilos in forum New To Java
    Replies: 10
    Last Post: 08-18-2011, 09:02 PM
  3. iterate through a HashMap ?
    By aneuryzma in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 04:26 PM
  4. How to iterate through two Arrays?
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 05-18-2010, 08:48 AM
  5. Replies: 0
    Last Post: 05-18-2010, 01:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •