gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:37:46 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com