我有一个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