views:

317

answers:

7

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?
+7  A: 

You simply forgot to "move" to the next element:

while (itr.hasNext()) {
   // Get next element
   Object o = itr.next();
   // do what you want with your element...
}

Note that since Java 5, you can iterate on a list using this syntax:

List<String> myList = getAListOfString();
for (String s : myList) {
    // Your code
}
romaintaz
+4  A: 

You have to call next():

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

On a side note, I'd strongly recommend you adopt the Java coding style (as in my example with opening braces on the same line).

You can use an Iterator but there usually isn't much point. Since Java 5 you can do this:

public String listAll() {
  for (Object ob : arrBok) {
    i++;
  }
  return "lol";
}

And for any List you've always been able to do this:

public String listAll() {
  for (int i=0; i<arrBok.length(); i++) {
    Object ob = arrBok.get(i);
  }
  return "lol";
}

which is efficient for ArrayList but not for LinkedList.

cletus
+10  A: 

That code as quoted won't compile, you're missing a couple of declarations. But I think I get the gist.

hasNext tells you that the iterator has a next value, but doesn't take you to it. To do that, you use next. So that is an endless loop because you're never calling next.

With any recent compiler, you can use the much simpler notation for this:

for (Object book : arrBok) {
    // ...do something with 'book' (or just increment your counter)...
}

That iterates through the collection (list, in this case) for you.

T.J. Crowder
Ahhhh! Thank you :]
cc0
+2  A: 

try this:

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

you didn't actually iterate it. just check for next.

Andrey
+6  A: 

why not iterate like this:

for(Book book :arrBok){
  // do something
}
kukudas
I would think this is best way if you are able to use Java 5 for-each loops and generics.
Mark McLaren
A: 

Make sure you call itr.next() in the loop to move to the next element.

Also, i++ has no relevance in your code - it is just incrementing a variable that is not being used anywhere.

Robert Conn
A: 

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

Yes, that makes sense. If you want to iterate through all items, you need to call itr.hasNext() followed by call to itr.next(). However, if you do return itr.next().toString() inside the loop, it cuts the loop short on the first iteration, and returns from the method immediately. So, do like cletus said.

Jonik