Comparing two integer arrays in Java(比较Java中的两个整数数组)
问题描述
我正在尝试编写代码来比较两个数组.在第一个数组中我放了自己的数字,但第二个数组从输入文件中获取数字.此数组的大小由文件中的第一个数字决定,而第一个数组的大小始终为 10.两个数组的长度必须相同,数字也必须相同.
I am trying to write code to compare two arrays. In the first array I have put my own digits, but the second the array takes numbers from the input file. The size of this array is determined by the first number in the file while the first array is always of size 10. The length must be the same for both arrays as well as the numbers.
我的代码如下:
public static void compareArrays(int[] array1, int[] array2) {
boolean b = false;
for (int i = 0; i < array2.length; i++) {
for (int a = 0; a < array1.length; a++) {
if (array2[i] == array1[a]) {
b = true;
System.out.println("true");
} else {
b = false;
System.out.println("False");
break;
}
}
}
}
推荐答案
public static void compareArrays(int[] array1, int[] array2) {
boolean b = true;
if (array1 != null && array2 != null){
if (array1.length != array2.length)
b = false;
else
for (int i = 0; i < array2.length; i++) {
if (array2[i] != array1[i]) {
b = false;
}
}
}else{
b = false;
}
System.out.println(b);
}
这篇关于比较Java中的两个整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:比较Java中的两个整数数组


基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java Swing计时器未清除 2022-01-01