Java程序比较两个Java短数组
要比较两个Java短数组,请使用Arrays.equals()方法。
让我们首先声明并初始化一些短数组。
short[] arr1 = new short[] { 20, 15, 35, 55, 69 }; short[] arr2 = new short[] { 20, 15, 35, 55, 69 }; short[] arr3 = new short[] { 22, 19, 30, 45, 78 };
现在让我们比较以上两个数组。
Arrays.equals(arr1, arr2));
同样,将其用于其他数组并进行比较
示例
import java.util.*; public class Demo { public static void main(String []args) { short[] arr1 = new short[] { 20, 15, 35, 55, 69 }; short[] arr2 = new short[] { 20, 15, 35, 55, 69 }; short[] arr3 = new short[] { 22, 19, 30, 45, 78 }; System.out.println(Arrays.equals(arr1, arr2)); System.out.println(Arrays.equals(arr2, arr3)); System.out.println(Arrays.equals(arr1, arr3)); } }
输出结果
true false false