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中的两个整数数组


- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01