简体   繁体   English

Java - 迭代ArrayList的问题

[英]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). 好的,所以我有一个ArrayList (arrBok) ,里面装满了book对象(代码是挪威语,所以请不要注意这一点)。 I want to make a public method which iterates through all the objects in the `ArrayList'. 我想创建一个遍历`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. 如果bokArr (ArrayList)为空,则进行测试,但不是。 It has 4 objects inside of it. 它里面有4个物体。

  • Return the toString() method of the itr , with the following result; 返回itrtoString()方法,结果如下:

java.util.AbstractList$Itr@173a10f // <-- not sure if this would be relevant to anything java.util.AbstractList$Itr@173a10f // < - 不确定这是否与任何内容相关

  • 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. hasNext告诉您迭代器具有下一个值,但不会将您带到它。 To do that, you use next . 要做到这一点,你使用next So that is an endless loop because you're never calling next . 所以这是一个无限循环,因为你永远不会打电话给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. 这会为您迭代集合(在本例中为list)。

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: 请注意,从Java 5开始,您可以使用以下语法迭代列表:

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() : 你必须调用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). 另外,我强烈建议你采用Java编码风格(如我的例子中,在同一行上打开括号)。

You can use an Iterator but there usually isn't much point. 你可以使用Iterator通常没什么意义。 Since Java 5 you can do this: 从Java 5开始,您可以这样做:

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

And for any List you've always been able to do this: 对于任何List您始终能够执行此操作:

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 . 这对ArrayList有效,但对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. 确保在循环中调用itr.next()以移动到下一个元素。

Also, i++ has no relevance in your code - it is just incrementing a variable that is not being used anywhere. 此外,i ++与您的代码无关 - 它只是递增一个未在任何地方使用的变量。

return itr.next().toString(); 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() . 如果要遍历所有项目,则需要调用itr.hasNext() 然后调用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. 但是,如果在循环内return itr.next().toString() ,它会在第一次迭代时缩短循环次数,并立即从方法返回。 So, do like cletus said . 所以,像克莱图斯说的那样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM