我有一個arraylist包含一些值的順序(正確的位置),現在我想滑動/設置/手動shuffle更改爲arraylist混亂的值。下圖可以給我的想法。將Array-II更改爲Array-I。爲了洗牌,我們可以使用collection.shuffle(),但是在這裏我有一個文本文件,在那個文本文件中,我寫了一個原始位置以及陣列中數據的當前位置到文本文件中。例如成像,我們有一個文本文件名MyFile.txt的,我在像這樣寫數據, 「original_position〜CURRENT_POSITION」 例如 0〜0 1〜2 2〜4 3〜3 4〜1 所以,基於上面的數據我會洗牌/設置陣列。如何手動洗牌數組列表
5
A
回答
2
在這裏,我發現解決方案,我的問題請觀察這裏。 「itemList_dump」是一個包含數據的數組列表。
if (i < itemList_dump.size()) {
MyBitamp Data = itemList_dump.get(original_position_val);
// setting Values according to our algorithm
Data.setItemCurPos(original_position_val);
Data.setItemPosition(current_positon_val);
Data.setrotation(rotataion_val);
}
}
Collections.sort(itemList_dump, new Comparator<MyBitamp>() {
@Override
public int compare(MyBitamp o1, MyBitamp o2) {
if (o1.getItemPosition() > o2.getItemPosition()) {
return 1;
} else if (o1.getItemPosition() < o2.getItemPosition()) {
return -1;
} else {
return 0;
}
}
});
4
Collections.shuffle()
會爲你做它。 Collections.sort()
將按照自然順序(A,B,C,D,E ..)對它進行排序,如果您想對其進行排序。
7
使用Collections.shuffle(YOUR_ARRAY_LIST);
來做到這一點。
List<String> ranList = new ArrayList<String>();
// populate the list
ranList.add("A");
ranList.add("B");
ranList.add("C");
System.out.println("Initial collection: " + ranList);
// shuffle the list
Collections.shuffle(ranList);
System.out.println("Final collection after shuffle: " + ranList);
的人工分揀方法的方式是這樣的:
讀取和文件數據添加到Map
private static Map<String, String> readFile(String fileNameWithPath) throws Exception {
Map<String, String> map = new LinkedHashMap<String, String>();
File file = new File(fileNameWithPath);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String sCurrentLine;
String[] splitPosition;
while ((sCurrentLine = bufferedReader.readLine()) != null) {
splitPosition = sCurrentLine.split("~");
map.put(splitPosition[0], splitPosition[1]);
}
return map;
}
而且你的主要方法是這樣的:
try {
List<String> list = new ArrayList<String>();
List<String> customeRandomList = new LinkedList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
Map<String, String> map = Test.readFile("D://RandomList.txt");
for (Map.Entry<String, String> entry : map.entrySet()) {
customeRandomList.add(list.get(Integer.parseInt(entry.getValue())));
}
for (String result : customeRandomList) {
System.out.println(result);
}
} catch (Exception e) {
e.printStackTrace();
}
相關問題
- 1. 如何洗牌ImmutableJS列表
- 2. 如何洗牌活動列表
- 3. 如何洗牌數組值
- 4. 如何「洗牌」數組?
- 5. 如何洗牌整數數組?
- 6. 如何切片數組然後洗牌
- 7. 如何洗牌字符串數組
- 8. 如何洗牌INSPhotoViewable(自定義)數組?
- 9. 洗牌多維動態數組
- 10. c + +洗牌動態數組的內容?
- 11. 如何洗牌
- 12. SimpleAudioEngine洗牌播放列表
- 13. 隨機洗牌列表
- 14. 洗牌在Objective-C數組
- 15. 隨機洗牌數組
- 16. 洗牌數組元素
- 17. 洗牌Javascript數組優雅
- 18. C++:如何洗牌動態數組指針?
- 19. 如何洗牌對
- 20. 如何手動洗牌字符串中的字母
- 21. 在Python中隨機化/洗牌列表/數組?
- 22. 後洗牌一個列表獲取數組與jquery
- 23. 洗牌陣列javascript
- 24. 陣列洗牌java
- 25. Ember.js洗牌陣列
- 26. COUNTIF手動數組列表
- 27. 如何將一副牌洗牌成兩隻手
- 28. 如何從numpy數組創建洗牌批次列表以饋入tensorflow字典
- 29. 如何在洗牌後在數組列表中跟蹤我的正確答案?
- 30. 使用不顯示列表列表的牌洗牌的輸出
這可能是簡單的,我知道這一點,但這裏塞納里奧是不同的,我有數據的文本文件,該文本文件包含,original_position以及數組的CURRENT_POSITION的基礎上,我想在array-II中設置數據。即手動洗牌。 – user3285681
你的手動洗牌總是一樣的。因此,0,1,2,3,4應該總是被洗牌爲0,2,4,3,1 – ashokramcse
請您公開該示例文件。 – ashokramcse