How do you compare the same elements in two arrays in Java

  • 2020-04-01 03:22:06
  • OfStack

Hehe hehe, how do you do the Java function of comparing elements in two arrays? Here's the easiest way:
 
import java.util.Arrays; 
public class Test { 
 
public static void main(String[] args) { 
String [] array1 = {"1","2","3"}; 
String [] array2 = {"3","2","1"}; 
Arrays.sort(array1); 
Arrays.sort(array2); 
if (Arrays.equals(array1, array2)) { 
System.out.println(" The elements in both arrays have the same value "); 
} else { 
System.out.println(" The values of the elements in the two arrays are different "); 
} 
} 
} 

Related articles: