http://www.vini123.com/chatroom
判断两个数组相等,首先判断两个数组的长度是否相等。再判断数组里边的元素是否相等。我所想要判断的是数组长度一样,但是元素属性不一样。
看个人需要。我自己是需要第一种。
下边给出代码:
1 输出: true false false true 2 var arr1 = [5,6,7,8]; 3 var arr2 = [7,5,6,8]; 4 var arr3 = [7,5,6,2]; 5 var arr4 = [5,6,7,8]; 6 var aa = judgeArr(arr1,arr2); 7 var bb = judgeArr(arr1,arr3); 8 var cc = judgeAllArr(arr1,arr2); 9 var dd = judgeAllArr(arr1,arr4); 10 trace(aa,bb,cc,dd); 11 12 function judgeArr(arr1:Array,arr2:Array):Boolean 13 { 14 var len1:int = arr1.length; 15 var len2:int = arr2.length; 16 if (len1!=len2) 17 { 18 return false; 19 } 20 else 21 { 22 for (var i:int=0; i<len1; i++) 23 { 24 25 var len = arr1.indexOf(arr2[i]); 26 if (len<0) 27 { 28 return false; 29 } 30 31 } 32 return true; 33 } 34 } 35 36 //两个数组完全相等 37 function judgeAllArr(arr1:Array,arr2:Array):Boolean 38 { 39 var len1:int = arr1.length; 40 var len2:int = arr2.length; 41 if (len1!=len2) 42 { 43 return false; 44 } 45 else 46 { 47 for (var i:int=0; i<len1; i++) 48 { 49 if (arr1[i] != arr2[i]) 50 { 51 return false; 52 } 53 54 } 55 return true; 56 } 57 }