Java - problems iterating through an ArrayList
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 theitr
, 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?
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.
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
}
why not iterate like this:
for(Book book :arrBok){
// do something
}
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
.
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.
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.
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.
精彩评论