我有一個Java中的整數Arraylist調用列表,其中包含7個元素[12, 13, 17, 18, 19, 11, 20]
。我想用隨機順序在列表的最後複製相同的元素兩次,所以最後有三次相同的整數(總共21項),然而,每7項隨機順序。我如何在Java中這樣做?複製和隨機化列表中的項目的位置
回答
只需複製清單並隨機播放每次迭代。
final int DUPLICATE_AMOUNT = 3; // if you want the created array 3 times as big as the original
List<Integer> list = getMyList(); //original list
List<Integer> fullRandom = new ArrayList<Integer>();
fullRandom.addAll(list);
for (int i = 1; i < DUPLICATE_AMOUNT; i++) {
List<Integer> randomList = new ArrayList<Integer>();
randomList.addAll(list);
Collections.shuffle(randomList);
fullRandom.addAll(randomList);
}
我的初始列表是一個ArrayList。是否有可能首先將它轉換爲List以執行addAll? – konstantin
雖然技術上是正確的,但我發現你使用'DUPLICATE_AMOUNT'和'int i = 1'具有誤導性。 1)「重複」可以描述額外的副本或整體(如你所期望的)2)人們習慣於'int i = 0',以至於他們可能會錯過你的'int i = 1'並且誤解。你的答案沒有錯,但我認爲'ADDITIONAL_COPIES = 2' +'int i = 0'會更清楚。 – Aaron
@konstantin ArrayList是'List'的一個實現,不需要投射。 – Aaron
只要複製列表兩次,將它洗:
List<Integer> tempList = new ArrayList<>(yourList);
for(int i = 0; i < 2; i++){
Collections.shuffle(tempList, new Random(System.nanoTime()));
yourList.addAll(tempList);
}
@Aaron Thx供您參考。我編輯過它。 – Christian
// init your list
List<Integer> initialList = new ArrayList<Integer>();
initialList.add(new Integer(12));
initialList.add(new Integer(13));
initialList.add(new Integer(14));
initialList.add(new Integer(15));
// create a new list that'll contain your random numbers
List<Integer> tripleList = new ArrayList<Integer>();
// triple your values
tripleList.addAll(initialList);
tripleList.addAll(initialList);
tripleList.addAll(initialList);
// randomize their order
Collections.shuffle(tripleList);
// until is empty get the top of the list with this command.
//A random number among your list
tripleList.remove(0);
嘗試使用Collections.shuffle()
,並調用它的兩倍初始數據列表:
private List<Integer> shufflePositions(List<Integer> data) {
Collections.shuffle(data);
return data;
}
public void solve() {
List<Integer> data = new ArrayList<>();
data.add(12);
data.add(13);
data.add(17);
data.add(18);
data.add(19);
data.add(11);
data.add(20);
List<Integer> result = new ArrayList<>();
result.addAll(data);
result.addAll(shufflePositions(new ArrayList<>(data)));
result.addAll(shufflePositions(new ArrayList<>(data)));
}
試試這個:
List<Integer> integers = new ArrayList<>(Arrays.asList(12, 13, 17, 18, 19, 11, 20));
for(int i=0; i<2; i++) {
List<Integer> IntegersClone = new ArrayList<>(integers);
Collections.shuffle(IntegersClone);
integers.addAll(IntegersClone);
}
//Output : [12, 13, 17, 18, 19, 11, 20, 19, 17, 12, 11, 20, 18, 13, 20, 12, 19, 11, 18, 13, 13, 11, 17, 18, 19, 12, 17, 20]
System.out.print(integers);
- 1. 隨機隨機化項目列表
- 2. 衰落列表項的隨機位置
- 3. GridView複製隨機項目
- 4. 從列表中的隨機項目
- 5. 詞典:隨機密鑰和隨機數從鍵的列表中的項目
- 6. 在Knockout.js中隨機化Foreach列表項
- 7. 隨機位置的陣列
- 8. 隨機化元素位置到陣列
- 9. 在列表中生成隨機項目
- 10. 隨機化ArrayList中的項目子集
- 11. 隨機化對象位置
- 12. 製作JavaScript隨機工具,消費者從列表中選擇項目,隨後將其變爲隨機化
- 13. C# - 隨機化Linq項目
- 14. 隨機追加列表中的項目到矩陣列表
- 15. 改變列表中的項目位置
- 16. 從列表中拾取隨機項目,而不重複
- 17. 第一個列表項隨機變化
- 18. 爲什麼RecyclerView項目會隨機移動GridLayoutManager中的位置?
- 19. 隨機實例化3個不同位置的預製裝置
- 20. 從Google表格中的列表中選擇隨機項目
- 21. 隨機化動畫對象的位置
- 22. 如何隨機化的位置
- 23. UL中列表項的隨機(項目符號)圖像?
- 24. PowerShell複製項遠程機器複製到錯誤的位置
- 25. C++隨機項目關閉列表
- 26. Prolog中的隨機項目
- 27. XSLT中的隨機項目
- 28. 隨機化對象的位置沒有重複或重疊
- 29. 在Redis中存儲巨大的類似項目的隨機化列表
- 30. 如何從Android應用程序中的列表視圖中隨機化項目?
我不明白你的問題,你打算以21 int列表結束,還是隻是在其他地方複製你隨機選擇的數字? – rakwaht
是否重要,如果我們先複製它們然後隨機化它們,或者你想要它,因爲你複製它同時隨機化。 –
您可以將元素複製到另一個ArrayList,您將在其中執行'Collections.shuffle(copyList);'然後您將這些混合副本添加到原始數組列表中。 – matoni