public static void main(String[] args) { // 比较两个数组是否相等 int num1,num2; Scanner input=new Scanner(System.in); System.out.println("请输入第一组数组的个数"); num1=input.nextInt(); System.out.println("请输入第二组数组的个数"); num2=input.nextInt(); int[] a=new int[num1]; int[] b=new int[num2]; if(num1!=num2){ System.out.println("两组数组不相等"); return; } for (int i = 0; i < num1; i++) { System.out.println("请输入数组一的第"+(i+1)+"个数"); a[i]=input.nextInt(); }//输入数组a for (int i = 0; i < num2; i++) { System.out.println("请输入数组二的第"+(i+1)+"个数"); b[i]=input.nextInt(); }//输入数组b
for (int i = 0; i < num1; i++) { if(a[i]!=b[i]){ System.out.println("不相等"); return;} } System.out.println("两组数组相等"); }
也可以用java.util.Arrays包里面Arrays.equals()方法来直接比较
System.out.println(Arrays.equals(a,b));标签:相等,num1,int,是否,System,数组,println,out From: https://www.cnblogs.com/Liku-java/p/16788302.html