如何对列表进行混排? [英] How to unshuffle a list?
问题描述
我正在使用 Collections.shuffle(list);
来整理列表,但是我不知道如何去整理它?我在考虑保存重新整理列表,然后重新整理列表,以便可以维护备份并可以在需要时恢复它,但这似乎是一种低效的方式,并且会占用时间和内存....如果您知道更合理的方法这样做,请您详细说明一下吗?
就是我的应用程序的外观:D
I am using Collections.shuffle(list);
to shuffle a list but I don't know how to unshuffle it?I was thinking of saving the list before shuffling and then shuffle it so that a backup is maintained and can re restored whenever required ,but this seems like a inefficient way of doing it and will take up time and memory ....if you know a more logical way of doing it,can you please elaborate it ??
by the way here is how my app looks :D
推荐答案
没有这样的改组概念-在洗完一副纸牌后,您将如何例如,返回到先前的状态?
There's no such concept of unshuffling - after you've shuffled a deck of cards, how would you get back to the previous state, for example?
如果您以某种确定的方式订购了原始收藏,请再次对其进行排序。否则(例如,如果是手工订购的),您必须先进行复印,然后再洗。
If your original collection is ordered in some definitive way, just sort it again. Otherwise (e.g. if it's been hand-ordered) you must take a copy before you shuffle.
理论上您可以 :
- 生成随机种子并记住它
- 创建
随机
,然后将其传递到shuffle
- 稍后,创建
ArrayList<整数>
从0到大小(不包括大小) - 使用新的
Random
随机排序列表原始种子 - 使用结果计算出每个项目的原始索引(因为您知道每个原始项目在改组后的列表中的位置)
- Generate a random seed and remember it
- Create a
Random
and pass that intoshuffle
- Later, create an
ArrayList<Integer>
from 0 to size (exclusive) - Shuffle that list with a new
Random
created with the original seed - Use the results to work out the original index of each item (because you know where each original item ended up in the shuffled list)
...但这工作量很大。除非您的集合真的太大而不能保留多余的副本(别忘了它只是引用的副本,而不是整个对象的副本),否则我只会在拖曳之前克隆该集合。
... but that's an awful lot of work. Unless your collection is really too big to keep the extra copy (don't forget it's just a copy of references, not whole objects), I'd just clone the collection before shuffling.
这篇关于如何对列表进行混排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!