java 数组 洗牌_java – 如何手动洗牌数组列表

使用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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值