-1

I'm a long time surfer, first time asker.

I started teaching myself java a month ago with almost no programming experience (other than GUI based programming for Lego Mindstorms kits).

I'm testing a program that involves integer arrays that are filled with random numbers. I have to ensure that none of the arrays are equal. So, I went with a while loop that won't end until the comparison check for all arrays is complete. Here's the test code I'm using:

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

I get same valued integer arrays despite this. What am I doing wrong? Should I try something different.

Thank you for your help.

Regards

  • Step 1. Learn about loops. You'll be amazed with what they can do for you. I know I was when I wrote my first loop in 1983. – Marko Topolnik Feb 06 '14 at 10:13
  • 1
    "I'm a long time user, first time asker." - But "member for today" :/ – Maroun Feb 06 '14 at 10:13
  • @MarkoTopolnik In 1983 I wrote my first loop too, although I was born in 1985. I still don't know how that happened. – Maroun Feb 06 '14 at 10:14
  • 1
    @ᴍarounᴍaroun You see, they can even let you travel back through time! That's what I'm saying :) – Marko Topolnik Feb 06 '14 at 10:15
  • 1
    @ᴍarounᴍaroun - I guess by "user" the OP meant "surfer/reader" on SO :) – Rahul Feb 06 '14 at 10:15
  • 1
    @Ɍ.Ɉ He should be accurate, I'm here waiting for mistakes ;) – Maroun Feb 06 '14 at 10:16
  • You could try looking at this answer which seems to do exactly what you want: http://stackoverflow.com/questions/2496774/how-do-i-randomly-fill-an-array-in-java?rq=1 – mikea Feb 06 '14 at 10:19
  • @mikea no. It is not the same. – aalku Feb 06 '14 at 10:31
  • @user270349 not identical, no, but he was asking for something different and the answer to that question gives an alternative solution to randomly filling arrays. – mikea Feb 06 '14 at 10:33

1 Answers1

0

These 2 options check if the object is the same, i.e. is it the same array.

array1 == array2

and

array1.equals(array2)

To compare the contents of the arrays you need to use:

Arrays.equals(array1, array2) 
M21B8
  • 1,867
  • 10
  • 20