Java、严格相同的二维数组

该程序定义了一个方法`equals`用于检查两个二维整数数组是否严格相等,即对应元素完全相同。用户被提示输入两个3x3的整数矩阵,程序将比较这两个矩阵并显示它们是否严格相同。`equals`方法通过遍历数组并使用`Arrays.equals`进行内部比较实现这一功能。
摘要由CSDN通过智能技术生成

        如果两个二维数组m1和m2相应的元素相等的话,则认为它们是严格地相同的。编写一个方法,如果m1和m2是严格相同的话,返回true。使用下面的方法头:
        public static boolean equals(int[][] m1, int[][] m2)
编写一个测试程序,提示用户输入2个3*3的整数数组,显示两个矩阵是否是一个相同的。


package pack2;

import java.util.Arrays;
import java.util.Scanner;

public class StrictSameArrays {

	public static void main(String[] args) {
		strictSameArrays();
	}
	
//	51 22 25 6 1 4 24 54 6
//	51 25 22 6 1 4 24 54 6

	public static void strictSameArrays() {
		try(Scanner input = new Scanner(System.in);) {
			System.out.print("Enter the size of two arrays for the length of " + 
                    "row and column: ");
			int size = input.nextInt();
			
			int[][] list1 = new int[size][size];
			int[][] list2 = new int[size][size];
			
			System.out.print("Enter list1: ");
			inputData(input, list1);
			System.out.print("Enter list2: ");
			inputData(input, list2);
			
			System.out.println("The two arrays are " + (equals(list1, list2) ? "" :
                         "not ") + "strictly identical");
		}
	}
	
	/**接收数据输入*/
	public static void inputData(Scanner input, int[][] m) {
		for (int i = 0; i < m.length; i++) 
			for (int j = 0; j < m[i].length; j++) 
				m[i][j] = input.nextInt();
	}
	
	/**两个数组是否严格相同?*/
	public static boolean equals(int[][] m1, int[][] m2) {
		for (int i = 0; i < m2.length; i++) 
			if(!(Arrays.equals(m1[i], m2[i])))
				return false;
		return true;
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值