dimanche 28 décembre 2014

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.


Aucun commentaire:

Enregistrer un commentaire