Java语言程序设计 7.26(完全相同的数组)

7.26 (Strictly identical arrays) The arrays list1 and list2 are strictly identicalif their corresponding elements are equal. Write a method that returns true if list1 and list2 are strictly identical, using the following header:

public static boolean equals(int[] list1, int[] list2)

Write a test program that prompts the user to enter two lists of integers and displayswhether the two are strictly identical. Here are the sample runs. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list.

7.26(完全相同的数组)如果数组list1和list2的对应元素相等,则它们是完全相同的。编写一个方法,如果list1和list2完全相同,则使用以下标头返回true:

public static boolean equals(int[] list1, int[] list2)

编写一个测试程序,提示用户输入两个整数列表,并显示两者是否完全相同。以下是运行示例。请注意,输入中的第一个数字表示列表中元素的数量。此号码不在列表中。

import java.util.Scanner;
public class Unite7Test26 
{
	public static void main(String[] args) 
	{
		Scanner input = new Scanner(System.in);
		int []List1 =new int [6];
		int []List2 =new int [6];
		System.out.println("请输入第一个数组列:");
		for(int i = 0;i<=5;i++) 
		{
			int n = input.nextInt();
			List1[i] = n;
		}
		System.out.println("请输入第二个数组列:");
		for(int i = 0;i<=5;i++) 
		{
			int n = input.nextInt();
			List2[i] = n;
		}
		if(equals(List1,List2) == true) 
		{
			System.out.println("两个数组相同");
		}else 
		{
			System.out.println("两个数组不相同");
		}
	}
	public static boolean equals(int[] list1, int[] list2) 
	{
		for(int i = 0;i <= 5;i++) 
		{
			if(list1[i]!= list2[i]) 
			{
				return false;
			}
		}
		return true;
	}
}

结果如下:

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

差劲的厉害了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值