Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing arrays in java

int [] nir1 = new int [2];
nir1[1] = 1;
nir1[0] = 0;


int [] nir2 = new int [2];
nir2[1] = 1;
nir2[0] = 0;

boolean t = nir1.equals(nir2);
boolean m = nir1.toString().equals(nir2.toString());

Why are both m and t false? What is the correct way to compare 2 arrays in Java?

like image 542
Unknown user Avatar asked Apr 07 '11 22:04

Unknown user


1 Answers

Use Arrays.equals method. Example:

boolean b = Arrays.equals(nir1, nir2); //prints true in this case
like image 141
CoolBeans Avatar answered Sep 24 '22 20:09

CoolBeans