用JAVA自定义个方法,比较二维数组的内容是否相等
答案:3 悬赏:0 手机版
解决时间 2021-11-16 19:35
- 提问者网友:夢醒日落
- 2021-11-16 04:22
用JAVA自定义个方法,比较二维数组的内容是否相等
最佳答案
- 五星知识达人网友:一秋
- 2021-11-16 06:01
开了一个又一个,别人又不是没回答你。其实这东西完全可以自己去写,自己查查api就搞定了~
补充!!!!!!!!!!这个是可用的
import java.util.Arrays;
public class Test_1 {
public static void main(String args[]) throws {
int a[][]={{1,2},{2,3}};
int b[][]={{1,2},{2,2}};
Test_1 test=new Test_1();
if(test.test(a, b)){
System.out.println("ok");
}else{
System.out.println("不相等");
}
}
public boolean test(int[][] a, int b[][]) {
if (a.length != b.length) {
return false;
} else {
for (int i = 0; i < a.length; i++) {
if (a[i].length != b[i].length) {
return false;
} else {
if (!Arrays.equals(a[i], b[i])) {
return false;
}
}
}
}
return true;
}
}
补充!!!!!!!!!!这个是可用的
import java.util.Arrays;
public class Test_1 {
public static void main(String args[]) throws {
int a[][]={{1,2},{2,3}};
int b[][]={{1,2},{2,2}};
Test_1 test=new Test_1();
if(test.test(a, b)){
System.out.println("ok");
}else{
System.out.println("不相等");
}
}
public boolean test(int[][] a, int b[][]) {
if (a.length != b.length) {
return false;
} else {
for (int i = 0; i < a.length; i++) {
if (a[i].length != b[i].length) {
return false;
} else {
if (!Arrays.equals(a[i], b[i])) {
return false;
}
}
}
}
return true;
}
}
全部回答
- 1楼网友:廢物販賣機
- 2021-11-16 06:55
public class ArrayEquals {
//通用类型处理方法,T为除基本类型以外的所有类型,T必须覆写 equals()方法.
public static boolean deepEquals(T[][] a,T[][] b){
try{
for(int i=0; i if(!java.util.Arrays.deepEquals(a[i],b[i]))
return false;
return true;
}catch(Exception e){}
return false;
}
//基本类型处理方法,这里是int类的,其它基本类型方法雷同
public static boolean deepEquals(int[][] a,int[][] b){
try{
for(int i=0; i for(int j=0; j if(a[i][j]!=b[i][j])
return false;
return true;
}catch(Exception e){}
return false;
}
static void a(Object [] a){}
public static void main(String[] args) {
//基本类型测试:
int[][] a = {{1,2,3},{2,3,4},{3,4,5,6,7,8}};
int[][] b = {{1,2,3},{2,3,4},{3,4,5,6,7,8}};
int[][] c = {{1,2,3},{2,3,5},{3,4,5,6,7,8}};
boolean equals;
equals = deepEquals(a,b);
if(equals)System.out.println("a和b完全一样");
else System.out.println("a和b不完全一样");
equals = deepEquals(a,c);
if(equals)System.out.println("a和b完全一样");
else System.out.println("a和c不完全一样");
//非基本类型测试
E[][] t1 = {{new E(1),new E(2)},{new E(3),new E(4)},{new E(56),new E(77)},};
E[][] t2 = {{new E(1),new E(3)},{new E(3),new E(4)},{new E(56),new E(77)},};
E[][] t3 = {{new E(1),new E(2)},{new E(3),new E(4)},{new E(56),new E(77)},};
equals = deepEquals(t1,t2);
if(equals)System.out.println("t1和t2完全一样");
else System.out.println("t1和t2不完全一样");
equals = deepEquals(t1,t3);
if(equals)System.out.println("t1和t3完全一样");
else System.out.println("t1和t3不完全一样");
}
}
class E{
int id;
E(int i){id=i;}
public boolean equals(Object o){
return o!=null&&o instanceof E?((E)o).id==id:false;
}
}
//通用类型处理方法,T为除基本类型以外的所有类型,T必须覆写 equals()方法.
public static
try{
for(int i=0; i if(!java.util.Arrays.deepEquals(a[i],b[i]))
return false;
return true;
}catch(Exception e){}
return false;
}
//基本类型处理方法,这里是int类的,其它基本类型方法雷同
public static boolean deepEquals(int[][] a,int[][] b){
try{
for(int i=0; i for(int j=0; j if(a[i][j]!=b[i][j])
return false;
return true;
}catch(Exception e){}
return false;
}
static void a(Object [] a){}
public static void main(String[] args) {
//基本类型测试:
int[][] a = {{1,2,3},{2,3,4},{3,4,5,6,7,8}};
int[][] b = {{1,2,3},{2,3,4},{3,4,5,6,7,8}};
int[][] c = {{1,2,3},{2,3,5},{3,4,5,6,7,8}};
boolean equals;
equals = deepEquals(a,b);
if(equals)System.out.println("a和b完全一样");
else System.out.println("a和b不完全一样");
equals = deepEquals(a,c);
if(equals)System.out.println("a和b完全一样");
else System.out.println("a和c不完全一样");
//非基本类型测试
E[][] t1 = {{new E(1),new E(2)},{new E(3),new E(4)},{new E(56),new E(77)},};
E[][] t2 = {{new E(1),new E(3)},{new E(3),new E(4)},{new E(56),new E(77)},};
E[][] t3 = {{new E(1),new E(2)},{new E(3),new E(4)},{new E(56),new E(77)},};
equals = deepEquals(t1,t2);
if(equals)System.out.println("t1和t2完全一样");
else System.out.println("t1和t2不完全一样");
equals = deepEquals(t1,t3);
if(equals)System.out.println("t1和t3完全一样");
else System.out.println("t1和t3不完全一样");
}
}
class E{
int id;
E(int i){id=i;}
public boolean equals(Object o){
return o!=null&&o instanceof E?((E)o).id==id:false;
}
}
- 2楼网友:独钓一江月
- 2021-11-16 06:18
- -#
public class A{
public static void main(String[] args){
int a=10;
int b=20;
if(a==b)
System.out.prinln("a等于b");
else
System.out.prinln("a不等于b");
}
}
}
public class A{
public static void main(String[] args){
int a=10;
int b=20;
if(a==b)
System.out.prinln("a等于b");
else
System.out.prinln("a不等于b");
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯