怎样将多个值放到一个数组了,然后怎么比较两个数组是否相等?
大家帮帮吗?
怎样将多个值放到一个数组里(我通过循环得到数值),然后怎么比较两个数组是否相等?
------解决方案--------------------调用equals方法
或者手动for循环比较
如果元素的顺序无关紧要,那Set会更加合适
------解决方案--------------------Arrays.equals()
------解决方案--------------------indexof
------解决方案--------------------class ArrayEquals{
public static boolean isArrayEquals(Object[] array1,Object[] array2){
if( array1.length != array2.length )
return false;
for(int i=0 ; i <array1.length ; i++){
if( !array1[i].equals(array2[i]) )
return false;
}
return true;
}
public static void main(String[] args){
String[] arr1 = new String[]{ "1 ", "2 ", "3 ", "4 "};
String[] arr2 = new String[]{ "1 ", "2 ", "3 ", "4 "};
String[] arr3 = new String[]{ "1 ", "2 ", "4 ", "3 "};
System.out.println(ArrayEquals.isArrayEquals(arr1,arr2));
System.out.println(ArrayEquals.isArrayEquals(arr1,arr3));
System.out.println(ArrayEquals.isArrayEquals(arr3,arr2));
}
}
------解决方案--------------------mark
------解决方案--------------------ding....