2014-02-06 29 views
-1

我是一个很长时间的冲浪者,第一次提问。确保随机生成的int []数组不等于

我一个月前开始自学java,几乎没有编程经验(除了基于GUI的Lego Mindstorms套件编程)。

我正在测试一个涉及随机数的整数数组的程序。我必须确保没有任何数组是平等的。所以,我去了一个while循环,直到所有数组的比较检查完成才会结束。以下是我正在使用的测试代码:

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)); 
    } 
} 

尽管如此,我仍然得到相同值的整数数组。我究竟做错了什么?我应该尝试不同的东西吗?

谢谢你的帮助。

问候

+0

第1步:了解循环。你会为他们能为你做什么感到惊讶。我知道我是在1983年写了第一个循环的时候。 –

+1

“我是一个很长时间的用户,第一次提问。” - 但是“今天的成员”:/ – Maroun

+0

@MarkoTopolnik 1983年,我也写了第一个循环,虽然我出生于1985年。我仍然不知道那是怎么发生的。 – Maroun

回答

0

这两个选项检查对象是相同的,即,它是在相同的阵列。

array1 == array2 

array1.equals(array2) 

比较,你需要使用数组的内容:

Arrays.equals(array1, array2) 
+0

感谢您的回复。我意识到我出错的地方。 – user3278994