Java 确保随机生成的int[]数组不是';不相等
我是一个很长时间的冲浪者,第一次询问 我一个月前开始自学java,几乎没有编程经验(除了乐高Mindstorms工具包基于GUI的编程) 我正在测试一个程序,它包含一个用随机数填充的整数数组。我必须确保所有数组都不相等。因此,我使用了一个while循环,直到所有数组的比较检查完成后才会结束。以下是我正在使用的测试代码:Java 确保随机生成的int[]数组不是';不相等,java,arrays,Java,Arrays,我是一个很长时间的冲浪者,第一次询问 我一个月前开始自学java,几乎没有编程经验(除了乐高Mindstorms工具包基于GUI的编程) 我正在测试一个程序,它包含一个用随机数填充的整数数组。我必须确保所有数组都不相等。因此,我使用了一个while循环,直到所有数组的比较检查完成后才会结束。以下是我正在使用的测试代码: import java.util.Arrays; public class testmain { public static void main (String[] ar
import java.util.Arrays;
public class testmain {
public static void main (String[] args){
int[] testint1 = new int[2];
int[] testint2 = new int[2];
int[] testint3 = new int[2];
int[] testint4 = new int[2];
boolean donecheck = false;
while (donecheck == false){
testint1[0] = (int) (Math.random() * 4);
testint1[1] = (int) (Math.random() * 4);
testint2[0] = (int) (Math.random() * 4);
testint2[1] = (int) (Math.random() * 4);
testint3[0] = (int) (Math.random() * 4);
testint3[1] = (int) (Math.random() * 4);
testint4[0] = (int) (Math.random() * 4);
testint4[1] = (int) (Math.random() * 4);
if (testint1 != testint2){
if (testint1 != testint3){
if (testint1 != testint4){
if (testint2 != testint3){
if (testint2 != testint4){
if (testint3 != testint4){
donecheck = true;
}
}
}
}
}
}
}
System.out.print (Arrays.toString(testint1));
System.out.print (Arrays.toString(testint2));
System.out.print (Arrays.toString(testint3));
System.out.print (Arrays.toString(testint4));
}
}
尽管如此,我还是得到了相同值的整数数组。我做错了什么?我应该试试别的吗
谢谢你的帮助
这两个选项检查对象是否相同,即是否为同一数组
array1 == array2
及
要比较需要使用的数组的内容,请执行以下操作:
Arrays.equals(array1, array2)
第一步。了解循环。你会惊讶于他们能为你做什么。当我在1983年写我的第一个循环时,我知道我是。我是一个长期用户,第一次询问者。-但是“今天的成员”:/@ᴍ阿伦ᴍ你看,他们甚至可以让你穿越时空!我就是这么说的:)@ᴍ阿伦ᴍaroun-我猜“用户”这个词的意思是“冲浪者/读者”,所以:)@Ɍ他应该是准确的,我在这里等着出错;)谢谢你的回复。我意识到我错在哪里了。
Arrays.equals(array1, array2)