java - 如何比较Arraylist中不同数组中的两个元素?

标签 java arrays arraylist

我使用 ArrayList (java) 创建了两个数组:aList1aList2。两者都将混合 double 和弦乐。如何直接将 aList1 的各个内容与 aList2 中相应的各个内容进行比较 例如,如果 aList1 中的第一个值或字符串不存在与 aList2 中的第一个值或字符串不匹配,应该有一条消息表明两者不匹配。对于每个 ArrayList 的每个元素都应该进行此操作。

谢谢!

编辑:

这是我最初的尝试:

if (!aList1.get(0).equals(aList2.get(0))) { 
     JOptionPane.showMessageDialog(null,  "#1 is incorrect.");
}
if (!aList1.get(1).equals(aList2.get(1))) {
     JOptionPane.showMessageDialog(null, "#2 is incorrect.");
}

依此类推,通过比较 aList1 和 aList2 中的每个元素,并查看它们是否不相等(无论它们是 double 还是字符串)。数组的相应元素和大小始终相同。例如,如果 aList1 = {0,1,2,3,4,dog},aList2 可以包含 {10,2,5,2,cat}。

编辑:整个代码。

import javax.swing.JOptionPane;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;

public class KevinMath2 {
        static File filename = new File("homework.txt");
        static ArrayList <Object> aList = new ArrayList <Object> ();
        static String homework = " ";
        static File filename2 = new File("homework2.txt");
        static ArrayList <Object> aList2 = new ArrayList <Object> ();
        static String homework2 = " ";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String initialInput = JOptionPane.showInputDialog(null, "Would you like to add answers or check answers?");
        String again;
        char repeat;
        do {
            switch (initialInput) {
                case "Add answers":
                    char answerfinal1;
                    String answerPass = JOptionPane.showInputDialog(null, "Please enter the password");
                    while (!answerPass.equals("Victor")) {
                        JOptionPane.showMessageDialog(null,  "Incorrect Password. Please try again.");
                        answerPass = JOptionPane.showInputDialog(null, "Please enter the password.");
                    }
                    do {
                        do {
                            String options = JOptionPane.showInputDialog(null, "Enter the date of the desired" +
                                                                            " answers to add (M/D/Y)");
                            switch (options) {
                                case "05/29/15":
                                    while (!homework.isEmpty()) {
                                        homework = JOptionPane.showInputDialog("Please enter the answers, in order. After "
                                                                                + "the last answer, leave the next answer" 
                                                                                + " blank, and click OK.");
                                        if (!homework.isEmpty()) aList.add(homework);
                                    }
                                    try {
                                        FileWriter fw = new FileWriter (filename);
                                        Writer output = new BufferedWriter (fw);
                                        int sz = aList.size();
                                        for (int i=0; i < sz; i++) {
                                            output.write(aList.get(i).toString() + "\n");
                                        }
                                        output.close();
                                    } catch (Exception e) {
                                        JOptionPane.showMessageDialog(null, "Oops! I cannot create that file.");
                                    }
                                    break;
                                default:
                                    JOptionPane.showMessageDialog(null,  "Please enter a valid date.");
                                    break;
                            }
                        } while (!homework.isEmpty());
                        String final1 = JOptionPane.showInputDialog(null, "Is this correct: " + aList.get(0) + " "
                                                                        + aList.get(1) + " " + aList.get(2) + " " + 
                                                                        aList.get(3) + " " + aList.get(4) + "? (Yes or No)");
                        answerfinal1 = final1.charAt(0);
                    } while (answerfinal1 == 'n' || answerfinal1 == 'N');

                    break;
                    //Need to store the array permanently
                case "Check answers": //Need to make it so it stores array of Kevin's answers permanently
                    char answerfinal2;
                    String checkPass = JOptionPane.showInputDialog(null, "Please enter the password");
                    while (!checkPass.equals("Kevin")) {
                        JOptionPane.showMessageDialog(null,  "Incorrect Password. Please try again.");
                        answerPass = JOptionPane.showInputDialog(null, "Please enter the password.");
                    }   
                    do {
                        do {
                            String options2 = JOptionPane.showInputDialog(null, "Enter the date of the desired" +
                                                                            " answers to check (M/D/Y)");
                            switch (options2) {
                                case "05/29/15":
                                    while (!homework2.isEmpty()) {
                                        homework2 = JOptionPane.showInputDialog("Please enter the answers, in order. After "
                                                                            + "the last answer, leave the next answer" +
                                                                             " blank, and click OK.");
                                        if (!homework2.isEmpty()) aList2.add(homework2);
                                    }
                                    try {
                                        FileWriter fw = new FileWriter (filename2);
                                        Writer output = new BufferedWriter (fw);
                                        int sz = aList2.size();
                                        for (int i=0; i < sz; i++) {
                                            output.write(aList2.get(i).toString() + "\n");
                                        }
                                        output.close();
                                    } catch (Exception e) {
                                        JOptionPane.showMessageDialog(null, "Oops! I cannot create that file.");
                                    }
                                    break;
                                default:
                                    JOptionPane.showMessageDialog(null,  "Please enter a valid date.");
                            }
                        } while (!homework2.isEmpty());
                        String final2 = JOptionPane.showInputDialog(null, "Is this correct: " + aList2.get(0) + " "
                                + aList2.get(1) + " " + aList2.get(2) + " " + 
                                aList2.get(3) + " " + aList2.get(4) + "? (Yes or No)");
                        answerfinal2 = final2.charAt(0);
                    } while (answerfinal2 == 'n' || answerfinal2 == 'N');

                    int i = 0; // counter variable


                    if (aList.size() == aList2.size()) { // Check if both lists are equal
                        for (Object obj : aList) { // iterate through any list
                            if (obj.getClass() == String.class) { // find if it's a string
                                if (!aList.get(i).equals(aList2.get(i))) {
                                    JOptionPane.showMessageDialog(null, "#" + i + " is wrong.");
                                }

                            }

                            if (obj.getClass() == Double.class) { // or a double
                                if (!aList.get(i).equals(aList2.get(i))) {
                                    JOptionPane.showMessageDialog(null, "#" + i + " is wrong.");
                                }
                            }

                            i++;
                        }
                    }

                    break;
                default:
                    JOptionPane.showMessageDialog(null, "Please enter a valid option.");
                    break;
                } 
            again = JOptionPane.showInputDialog(null, "Would you like to check another answer, or "+
                                                    "add another answer? (Yes or No)");
            repeat = again.charAt(0);
        } while (repeat == 'y' || repeat == 'Y');
        JOptionPane.showMessageDialog(null,  "Thanks for using this program");

    }

}

最佳答案

尝试使用 for 循环遍历第一个 ArrayList,并使用该 for 循环中的计数器(下面示例中的“i”)来比较使用 ArrayList 提供的 get 方法循环遍历的每个索引。

for (int i = 0; i < aList1.size(); i++) {
    if (!aList1.get(i).equals(aList2.get(i)))
        //output whatever you want it to say
}

编辑:建议改为 .equals 而不是 == 。很好的收获。

关于java - 如何比较Arraylist中不同数组中的两个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30550000/

相关文章:

Java - 使列表大小动态化

Java 为什么 DatagramPacket 的构造函数需要字节数组的长度?

java - 添加函数 void addRange(int[] array) 将新数组的元素放入现有数组的末尾?

java - 如何在 ArrayList 中存储然后访问类对象?

arrays - ArrayContains 未定义的冷融合

清除不需要的值

java - 在由用户定义的数据类型组成的数组列表中搜索

java - 如何禁用 REST-Assured 调试打印到控制台?

具有 ConcurrentHashmap 策略的 Java 多线程消费者

java - 使用键和值的软引用实现缓存