Java 示例 - 比较数组?
-
问题描述
如何比较两个数组? -
解决方案
下面的例子使用equals方法来检查两个数组是否存在。import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { int[] ary = {1,2,3,4,5,6}; int[] ary1 = {1,2,3,4,5,6}; int[] ary2 = {1,2,3,4}; System.out.println("Is array 1 equal to array 2?? " +Arrays.equals(ary, ary1)); System.out.println("Is array 1 equal to array 3?? " +Arrays.equals(ary, ary2)); } }
-
结果
上面的代码示例将产生以下结果。Is array 1 equal to array 2?? true Is array 1 equal to array 3?? false