使用Collections.shuffle(YOUR_ARRAY_LIST);去做吧.
List ranList = new ArrayList();
// 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 readFile(String fileNameWithPath) throws Exception {
Map map = new LinkedHashMap();
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 list = new ArrayList();
List customeRandomList = new LinkedList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
Map map = Test.readFile("D://RandomList.txt");
for (Map.Entry entry : map.entrySet()) {
customeRandomList.add(list.get(Integer.parseInt(entry.getValue())));
}
for (String result : customeRandomList) {
System.out.println(result);
}
} catch (Exception e) {
e.printStackTrace();
}