Collections.shuffle(cardsToSelect);//这句是什么意思
|
/** * 三、洗牌(Shuffle) * @param args */ public void shuffle(){ System.out.println("------------开始洗牌...---------------"); Collections.shuffle(cardsToSelect);//这句是什么意思 System.out.println("------------洗牌结束-------------"); //测试是否洗牌成功,同时给元素分配id int i = 0; for (Cards cards : cardsToSelect) { cards.id = i; //System.out.println(cards.id+":"+cards.name); i++; } } |
免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。
版权声明:作者保留权利,不代表本站立场。
|
|
|
|
|
|
|
@Testpublicvoidshuffle(){ListcardsToSelect=newArrayList();cardsToSelect.add(1);cardsToSelect.add(2);cardsToSelect.add(3);cardsToSelect.add(4);for(Integerinteger:cardsToSelect){System.out.print("洗牌之前的集合值:"+integer+"");}System.out.println();System.out.println("------------开始洗牌...---------------");Collections.shuffle(cardsToSelect);//这句是什么意思for(Integerinteger:cardsToSelect){System.out.print("洗牌之后的集合值:"+integer+"");}System.out.println();System.out.println("------------洗牌结束-------------");}

 |
|
|
|
|
|
|
|
shuffle洗牌的意思,他是Collections这个工具类的一个方法,见名知意,Collections.shuffle()
对括号中的集合进行随机打乱原来的顺序,也就是跟洗牌似的 |
|
|
|
|
|
|
|