Java 为什么';这不是环路中断吗?

Java 为什么';这不是环路中断吗?,java,arrays,while-loop,break,Java,Arrays,While Loop,Break,这里有一个用Java编写的while循环。目的是从列表中删除所有重复项但循环没有中断。可能还有其他错误 public static <E extends Comparable<? super E>> void removeDuplicates(ArrayList<E> L) { ArrayList<E> temp = new ArrayList<E>(L.size()); ArrayList<Integer>

这里有一个用Java编写的while循环。目的是从列表中删除所有重复项但循环没有中断。可能还有其他错误

public static <E extends Comparable<? super E>> void removeDuplicates(ArrayList<E> L) {
    ArrayList<E> temp = new ArrayList<E>(L.size());
    ArrayList<Integer> index = new ArrayList<>(L.size());
    int stop = 0;

    while (true) {
        //test for duplicates and save their indexes
        for (int i = 0; i < L.size(); i++) {
            if (!temp.contains(L.get(i))) {
                temp.add(L.get(i));
                index.add(i);
            }
        }
        // if there were duplicates they will be removed
        if (!index.isEmpty()) {
            stop = 1;
            for (int j = 0; j < index.size(); j++) {
                L.remove(index.get(j));
            }
        }
        //if nothing is removed there should be no duplicates and the loop should break
        if (stop == 0) {
            break;
        }
        index.clear();
        temp.clear();
        stop = 0;
    }

} 

publicstatic当临时列表中已经存在要删除的项时,需要更新这些项的列表。因此,索引列表将包含所有重复元素的索引:

public static <E extends Comparable<? super E>> void removeDuplicates(final ArrayList<E> L) {
final ArrayList<E> temp = new ArrayList<E>(L.size());
final ArrayList<Integer> index = new ArrayList<>(L.size());
int stop = 0;

while (true) {
  //test for duplicates and save their indexes
  for (int i = 0; i < L.size(); i++) {
    if (!temp.contains(L.get(i))) {
      temp.add(L.get(i));
    } else {

      index.add(i);
    }
  }
  // if there were duplicates they will be removed
  if (!index.isEmpty()) {
    stop = 1;
    for (int j = index.size() - 1; j >= 0; j--) {
      L.remove((int) index.get(j));
    }
  }
  //if nothing is removed there should be no duplicates and the loop should break
  if (stop == 0) {
    break;
  }
  index.clear();
  temp.clear();
  stop = 0;
}

publicstatic当临时列表中已经存在要删除的项时,需要更新这些项的列表。因此,索引列表将包含所有重复元素的索引:

public static <E extends Comparable<? super E>> void removeDuplicates(final ArrayList<E> L) {
final ArrayList<E> temp = new ArrayList<E>(L.size());
final ArrayList<Integer> index = new ArrayList<>(L.size());
int stop = 0;

while (true) {
  //test for duplicates and save their indexes
  for (int i = 0; i < L.size(); i++) {
    if (!temp.contains(L.get(i))) {
      temp.add(L.get(i));
    } else {

      index.add(i);
    }
  }
  // if there were duplicates they will be removed
  if (!index.isEmpty()) {
    stop = 1;
    for (int j = index.size() - 1; j >= 0; j--) {
      L.remove((int) index.get(j));
    }
  }
  //if nothing is removed there should be no duplicates and the loop should break
  if (stop == 0) {
    break;
  }
  index.clear();
  temp.clear();
  stop = 0;
}

公共静态谢谢。我以为while最后一行中的stop=0就可以了。现在我在没有它的情况下进行了测试,它实际上没有改变任何东西。仍然有错误;代码不会删除所有重复项。它只删除一个,其余的副本保留在列表中。请参考此更新的代码,您需要在else条件下更新索引列表。当您使用index删除元素时,将其强制转换为int,其他将调用带有对象输入参数的重载方法。现在它开始工作了!非常感谢。。。我如何在评论中使用@-符号?我把它和“Gaurav Jeswani”放在我评论的开头,但我在那里没有看到你的用户名。:D甚至我都不知道。我想你可以接受这个答案啊哈,这也是接受答案的标志。再次感谢您,非常感谢您帮助像我这样的初学者:)谢谢。我以为while最后一行中的stop=0就可以了。现在我在没有它的情况下进行了测试,它实际上没有改变任何东西。仍然有错误;代码不会删除所有重复项。它只删除一个,其余的副本保留在列表中。请参考此更新的代码,您需要在else条件下更新索引列表。当您使用index删除元素时,将其强制转换为int,其他将调用带有对象输入参数的重载方法。现在它开始工作了!非常感谢。。。我如何在评论中使用@-符号?我把它和“Gaurav Jeswani”放在我评论的开头,但我在那里没有看到你的用户名。:D甚至我都不知道。我想你可以接受这个答案啊哈,这也是接受答案的标志。再次感谢您,非常感谢您帮助像我这样的初学者:)为什么不使用when a by design?为什么不使用when a by design?