0

Why isn't this true?

int[][] arrayOfSets = {{1,2},{9,10},{1,2},{3,5}};
int[][] test =        {{1,2},{9,10},{1,2},{3,5}};

if(arrayOfSets==test){ //{{1,2},{9,10},{1,2},{3,5}}){
   System.out.println("Exactly the same");
}

The output should be "Exactly the same". or how can I compare 2 variables with 2dimensional arrays?

Jeggy
  • 1,474
  • 1
  • 19
  • 35

3 Answers3

2

To compare multidimensional arrays, use .deepEquals The link explains why

.deepEquals

And the following link explains why == or .equals doesn't work.

.equals definition

Community
  • 1
  • 1
1

You use == thats why failed, it's for identical of object check so use :

boolean check = Arrays.deepEquals(arrayOfSets, test);
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
0

This does not work because == compares the reference ie you can think of that as memory address..In this case since you are declaring 2 different arrays, their addresses are bound to be different.

One small suggestion since your array doesn't contain consecutive numbers such as {1,2}, {1,3} similarly.... you can instead use objects with 2 instance numbers and put those objects inside an array

FatherMathew
  • 960
  • 12
  • 15