为什么我的第二次循环不执行?

时间:2015-03-22 10:10:11

标签: java while-loop

public void compare(ArrayList list_old, ArrayList list_new) {
        try {
            Iterator<User> iterator_old = list_old.iterator();
            Iterator<User> iterator_new = list_new.iterator();

            //Check New User Is Added
            while (iterator_new.hasNext()) {
                Log.i("Test", "inside!");
                User user_new = iterator_new.next();
                boolean NEW = true;

                while (iterator_old.hasNext() && NEW) {
                    User user_old = iterator_old.next();
                    if (user_new.getUsername().equals(user_old.getUsername())) {
                        NEW = false;
                    }
                }
                if (NEW) {
                    generateNotification(getApplicationContext(), user_new.getUsername() + " has been added.");
                }
            }
            //Check User Is Removed
            while (iterator_old.hasNext()) {
                Log.i("Test", "inside");
                User user_old = iterator_old.next();
                boolean NEW = true;

                while (iterator_new.hasNext() && NEW) {
                    User user_new = iterator_new.next();
                    if (user_old.getUsername().equals(user_new.getUsername())) {
                        NEW = false;
                    }
                }
                if (NEW) {
                    generateNotification(getApplicationContext(), user_old.getUsername() + " has been removed.");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  1. 在这个函数中,我将旧列表与新列表进行比较。
  2. 第一次while循环检查,是添加的任何新用户,第二次是循环检查,是否删除了任何用户。
  3. 当我运行此应用程序时,我的第一个while循环执行,而它不会在循环时执行第二个。
  4. 我查看了Log.i(&#34; Test&#34;,&#34; inside!&#34;);我发现它不会进入我的第二次循环。
  5. 请帮忙

2 个答案:

答案 0 :(得分:2)

您需要重新初始化迭代器,

 public void compare(ArrayList list_old, ArrayList list_new) {
        try {
            Iterator<User> iterator_old;
            Iterator<User> iterator_new = list_new.iterator();

            //Check New User Is Added
            while (iterator_new.hasNext()) {
                Log.i("Test", "inside!");
                User user_new = iterator_new.next();
                boolean NEW = true;
                iterator_old = list_old.iterator();
                while (iterator_old.hasNext() && NEW) {
                    User user_old = iterator_old.next();
                    if (user_new.getUsername().equals(user_old.getUsername())) {
                        NEW = false;
                    }
                }
                if (NEW) {
                    generateNotification(getApplicationContext(), user_new.getUsername() + " has been added.");
                }
            }
            //initialize again
            iterator_old = list_old.iterator();
            //Check User Is Removed
            while (iterator_old.hasNext()) {
                Log.i("Test", "inside");
                User user_old = iterator_old.next();
                boolean NEW = true;
                 iterator_new = list_new.iterator();    
                while (iterator_new.hasNext() && NEW) {
                    User user_new = iterator_new.next();
                    if (user_old.getUsername().equals(user_new.getUsername())) {
                        NEW = false;
                    }
                }
                if (NEW) {
                    generateNotification(getApplicationContext(), user_old.getUsername() + " has been removed.");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

答案 1 :(得分:0)

您并未在任何地方重置迭代器,因此我认为任何代码都无法正常工作。如果你使用for-each循环,你的代码可能会工作并且更具可读性,所有Iterable类都支持。这将避免完全创建和重置迭代器的需要。

for (User newUser : list_new) {
    for (User oldUser : list_old) 
        // Compare
}

for (User oldUser : list_old) {
    for (User newUser : list_new) 
        // Compare
}