user3144079
user3144079

Reputation: 159

Array.equals(...) is giving incorrect result

Code That is not working

This code is should print the word 'inside if' but doesn't and I don't know what is wrong with it.

doublesArray[0] = 3;
doublesArray[1] = 3;
doublesArray[2] = 3;
doublesArray[3] = 0;

int[] temp6 = {3,3,3,0};
//length is 4 for both arrays

if(doublesArray.equals(temp6))
  System.out.println("Inside if");

These are things to show it should print true

      int[] temp6 = {3,3,3,0};
      doublesArray[0] = 3;      
      doublesArray[1] = 3;      
      doublesArray[2] = 3;      
      doublesArray[3] = 0;
      //length is 4 for both arrays

      System.out.println("temp6 " + temp6[0] + " " + temp6[1] + " " + temp6[2] + " " + temp6[3]);
      System.out.println("doublesArray " + doublesArray[0] + " " + doublesArray[1] + " " + 
        doublesArray[2] + " " + doublesArray[3]);
      System.out.println("This should be true: ");
      System.out.println("doublesArray.equals(temp6) = " + doublesArray.equals(temp6) + "\n");

      //testing
      if(doublesArray[0] == temp6[0])
      System.out.println("correct");
      if(doublesArray[1] == temp6[1])
      System.out.println("correct");
      if(doublesArray[2] == temp6[2])
      System.out.println("correct");
      if(doublesArray[3] == temp6[3])
      System.out.println("correct");

      //testing with numbers
      System.out.println(" ");
      if(doublesArray[0] == 3)
         System.out.println("CORRECT");
      if(doublesArray[1] == 3)
         System.out.println("CORRECT");
      if(doublesArray[2] == 3)
         System.out.println("CORRECT");
      if(doublesArray[3] == 0)
         System.out.println("CORRECT");

These are the results I got which should show that doublesArray.equals(temp6) = true

temp6 3 3 3 0
doublesArray 3 3 3 0
This should be true: 
doublesArray.equals(temp6) = false

correct
correct
correct
correct

CORRECT
CORRECT
CORRECT
CORRECT

Thank you for those that were able to help.

Upvotes: 0

Views: 177

Answers (3)

Denorm
Denorm

Reputation: 466

Arrays.equals(a1, a2) 

compares the contents of the two arrays which is what you want to use.

a1.equals(a2) 

compares the reference of a1 and a2 and it is basically the same as comparing if a1 and a2 are pointing to the same place in memory

Upvotes: 2

user3144079
user3144079

Reputation: 159

Answering own question so I get less views and others will get more I thought doublesArray.equals(temp6) should work but doesn't

Arrays.equals(temp6, doublesArray) works.

but don't why doublesArray.equals(temp6) is not giving an error if it doesn't work.

Upvotes: 0

Ankur Singhal
Ankur Singhal

Reputation: 26067

doublesArray.equals(temp6) is the same as array1 == array2, i.e. is it the same array.

Arrays.equals(array1, array2) compares the contents of the arrays.

 public static void main(String[] args) {
        int[] doublesArray = new int[4];
        doublesArray[0] = 3;
        doublesArray[1] = 3;
        doublesArray[2] = 3;
        doublesArray[3] = 0;

        int[] temp6 = { 3, 3, 3, 0 };
        // length is 4 for both arrays

        if (Arrays.equals(temp6, doublesArray)) {
            System.out.println("Inside if");
        }
    }

output

Inside if

Upvotes: 4

Related Questions