且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重新排序列表以在 Java 中随机化其顺序

更新时间:2023-11-23 15:43:28

打乱列表的***方法是使用标准库方法 Collections.shuffle.

The best way to shuffle a list is using the standard library method Collections.shuffle.

List lst = getListFromSomewhere();
Collections.shuffle(lst);

如果您想自己动手,请阅读 Fisher-Yates shuffle算法.

If you want to do it yourself, read up on the Fisher-Yates shuffle algorithm.