0

I am new to the site, for the record.
I have looked around but I have not found the answer that I want.

Let's say I have int[] arrayA and int[] arrayB and I'm letting the user input values for however many elements I set, five in this case.

So:

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int[] arrayA = new int[5];
    int[] arrayB = new int[5];
    for (i = 0; i < arrayA.length; i++)
    {
        arrayA[i] = input.nextInt();
    }
    for (i = 0; i < arrayB.length; i++)
    {
        arrayB[i] = input.nextInt();
    }
}

public static int[] arrayEquality(int[] a, int[] b)
{
    if (a[] != b[])
    {
        return false;
    }
}

If you can help it, please don't use anything beyond methods, arrays, and such. I still don't have an entirely good grip on what I'm learning in class.

  • Sort both arrays. Then you compare them for equality. And you can use [`Arrays.equals(int[], int[])`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#equals%28int[],%20int[]%29) for that. – Elliott Frisch Nov 13 '15 at 01:00
  • Possible duplicate of [Java: Checking equality of arrays (order doesn't matter)](http://stackoverflow.com/questions/10154305/java-checking-equality-of-arrays-order-doesnt-matter) – OneCricketeer Nov 13 '15 at 01:02

2 Answers2

0
    int ary1[] = {1, 4, 5, 8, 3, 2};
    int ary2[] = {1, 8, 2, 5, 4, 9};
    Arrays.sort(ary1);
    Arrays.sort(ary2);
    boolean b = Arrays.equals(ary1, ary2);
    System.out.println(b);
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
0

If you dont want to use Array.sort() , you can try the following

void checkEquality (int arrayA[] , int arrayB[]){
    boolean equals = false;
    for (int x = 0 ;x < 5 ; x++){
        equals = false;
        for (int y = 0 ; y < 5 ; y++){  
            if (arrayA[x] == arrayB[y]){
                equals = true;
                break;
            }
        }
        if (equals == false)
            return false;
    }
}
sean
  • 717
  • 2
  • 9
  • 23