简体   繁体   English

对于每个不执行的循环

[英]For each loops not executing

For some reason all the for-each loops in my method are completely ignored, and I cannot figure out why. 由于某种原因,我方法中的所有for-each循环都被完全忽略了,我无法弄清楚原因。 Here is my code: 这是我的代码:

private static boolean notUsed(ArrayList<Integer> check, ArrayList<ArrayList<Integer>> used)
  {
    boolean c1 = false;
    boolean c2 = true;
    for (ArrayList<Integer> item : used) // Not executed
    {
      System.out.println("It works!");
      Collections.sort(item);
      Collections.sort(check);
      if (check.equals(item)) c1 = true;
    }
    ArrayList<Integer> existing = new ArrayList<Integer>();
    for (int item : check)
    {
      for (int exists : existing) // Not executed
      {
        if (exists == item) 
        {
          c2 = false;
          break;
        }
        else existing.add(item);
      }
      if (c2 == false) break;
    }
    if (c1 && c2) return true;
    else return false;
  }

I have been going over it for the past 15 minutes and fail to see why the code refuses to bother with the contents of the loops. 在过去的15分钟里,我一直在研究它,而看不到为什么代码拒绝打扰循环的内容。 In fact, I didn't even know java could avoid executing loops before now. 实际上,我什至不知道Java在此之前可以避免执行循环。 Am I missing something obvious? 我是否缺少明显的东西?

Yes, you are missing something obvious. 是的,您缺少明显的东西。

If those loops aren't executing, then there are no elements in the arguments passed to the routine. 如果这些循环没有执行,则传递给例程的参数中没有元素。

You make a new ArrayList 您创建一个新的ArrayList

ArrayList<Integer> existing = new ArrayList<Integer>();

which is empty. 这是空的。

When the foreach runs, it has nothing to iterate over. 当foreach运行时,没有任何要迭代的内容。 Therefore, your "not executing" comment is perfectly valid. 因此,您的“未执行”注释完全正确。

The first loop however, depends on what exactly is passed. 但是,第一个循环取决于确切传递的内容。 Put some print statemtents figure out the size of the List. 放置一些打印状态提示以计算列表的大小。 Most likely, that is 0 as well, which again, causes the loop to be "skipped". 也很可能也是0,这再次导致循环被“跳过”。

的大小used必须在0和第二环路不执行,因为existing是一个新的ArrayList和其大小绝对是0

here is working code..this will give you idea. 这是工作代码..这会给你的想法。 As others mentioned..there would not be any elements to iterate...because i ran the below code and for each loop works... 正如其他人提到的那样..没有任何元素可以迭代...因为我运行了以下代码并且每个循环都有效...

import java.util.ArrayList;
import java.util.Collections;


public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> used = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer>item = new ArrayList<Integer>();
        item.add(1);
        used.add(item);
        notUsed(item,used );
    }

    private static boolean notUsed(ArrayList<Integer> check, ArrayList<ArrayList<Integer>> used)
      {
        boolean c1 = false;
        boolean c2 = true;
        for (ArrayList<Integer> item : used) // Not executed
        {
          System.out.println("It works!");
          Collections.sort(item);
          Collections.sort(check);
          if (check.equals(item)) c1 = true;
        }
        ArrayList<Integer> existing = new ArrayList<Integer>();
        for (int item : check)
        {
          for (int exists : existing) // Not executed
          {
            if (exists == item) 
            {
              c2 = false;
              break;
            }
            else existing.add(item);
          }
          if (c2 == false) break;
        }
        if (c1 && c2) return true;
        else return false;
      }

}

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

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