简体   繁体   English

Java是否在执行For循环,但同时对Linked List(“ For,While”)循环使用迭代器?

[英]Java doing a For loop but also using an iterator for Linked List ( “For, While”) Loop?

I apologize for the basic question but my newness to Java is causing me some frustration and I am unable to find an elegant way to do this from my searches. 我为基本问题表示歉意,但是我对Java的新颖性使我感到有些沮丧,并且无法从搜索中找到一种优雅的方式来做到这一点。

I want to iterate through a linked list using a For construct but also have an numerical iterator so that I can break the loop after a certain number of iterations. 我想使用For构造遍历链接列表,但也想拥有一个数字迭代器,以便在经过一定数量的迭代后可以中断循环。

I have this LL that I am iterating through: 我要遍历此LL:

LinkedList<SearchResult> docSearch;

I tried doing it like this but then only the iterator part worked (the result was always stuck on the first entry for each iteration) 我尝试这样做,但是只有迭代器部分有效(每次迭代的结果始终停留在第一个条目上)

for (SearchResult result : docSearch) while (iter2 < 50)  { 

//do stuff
iter2 = iter2 + 1;
}

Any advice is appreciated 任何建议表示赞赏

If you have to do that sort of checking, then I would just do it with an if in the block. 如果您必须进行这种检查,那么我只需在块中使用if进行检查。

for (SearchResult result : docSearch)  {
  if (iter2 >= 50) break;

  //do stuff
  iter2 += 1;
}

It will be better to use regular for..loop syntax to handle your need 最好使用常规的for..loop语法来满足您的需求

for (int i = 0; i < 50 && i < docSearch.getSize(); i++ ) {
    SearchResult result = docSearch.get(i);
}

Just because Java support for-each loop, does not mean we have to use it every time. 仅仅因为Java支持for-each循环,并不意味着我们必须每次都使用它。 I find using regular for..loop syntax is easier to read where your condition is isolated in 1 place. 我发现使用常规for..loop语法更容易阅读将条件隔离在1个地方的情况。 If you use for-each with break then you have 2 places which affect your code flow. 如果将for-each与break一起使用,则有2个地方会影响代码流。

where did you assign the value of iter2? 您在哪里分配了iter2的值?

try 尝试

for (SearchResult result : docSearch) 
{
  int iter2 = 0;
  while (iter2 < 50)  { 

  //do stuff
  iter2 = iter2 + 1;
  }
}

If you do this: 如果您这样做:

for (SearchResult result : docSearch) while (iter2 < 50)  { 

//do stuff
iter2 = iter2 + 1;
}

It's the exact same as doing this: 与执行此操作完全相同:

for (SearchResult result : docSearch) {

    while (iter2 < 50)  { 

    //do stuff
    iter2 = iter2 + 1;
    }
}

You can get around this in a number of ways. 您可以通过多种方式解决此问题。 One is a break (although some frown upon this as spaghetti code. 一个就是休息(尽管有些人对此不屑一顾是意大利面条式的代码。

for (SearchResult result : docSearch) { 
if(iter2 >= 50) break;
//do stuff
iter2 = iter2 + 1;
}

You can use a standard for loop and put the two conditions into the condition section 您可以使用标准的for循环并将这两个条件放入条件部分

Iterator<SearchResult> iter = docSearch.iterator();
for(SearchResult result = iter.next(); iter.hasNext() && iter2 < 50; result = iter.next()) {
   // do stuff
   iter2 = iter2 + 1;
}
for (SearchResult result : docSearch)  {
  if (iter2++ >= 50) break;
  //do stuff
}

Here might be a nice place for a post-incrementation too. 这也可能是后期增量的好地方。 :) :)

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

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