原理:隨機抽取幾個數,而後刪除指定下標。算法
問題:出現了重複。數組
//測試 var arr=["aa","bb","cc","dd","ee","ff"]; var xiabiao=[0,4,2]; for(var j=0;j<xiabiao.length;j++){ arr.splice(xiabiao[j],1); } trace(arr);
正確的算法 k爲第幾輪測試
//測試 結果是 bb dd ff var arr=["aa","bb","cc","dd","ee","ff"]; var xiabiao=[0,4,2]; xiabiao.sort(); var k=0; for(var j=0;j<xiabiao.length;j++){ arr.splice(xiabiao[j]-k,1); k++; }
一次刪除以後,這個數組的長度就發生了變化,下標不許確了,須要改變。spa