Java - problems iterating through an ArrayList

Posted by cc0 on Stack Overflow See other posts from Stack Overflow or by cc0
Published on 2010-03-16T13:41:24Z Indexed on 2010/03/16 13:46 UTC
Read the original article Hit count: 235

Filed under:
|
|
|

Ok so I have an ArrayList (arrBok), which is full of book objects (the code is in Norwegian, so pay no attention to that please). I want to make a public method which iterates through all the objects in the arraylist.

When I execute the code, it just seems to run in an infinite loop, not producing any return values.

Here is the relevant (I hope, because there are a couple of other classes involved) part of the code;

public String listAll()
{
    itr = arrBok.iterator();
    while (itr.hasNext())
    {
        i++;
    }
    return "lol";
}

This code does nothing useful, but I just want to see if it can iterate through it successfully.

What I have tried so far;

  • Tested if the bokArr (arraylist) is empty, which it's not. It has 4 objects inside of it.

  • Return the toString() method of the itr, with the following result;

java.util.AbstractList$Itr@173a10f // <-- not sure if this would be relevant to anything

  • return itr.next().toString(); <-- // which seems to return the first object in the array, does that make sense?

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays