如何手动洗牌阵列列表

use*_*681 5 java arraylist

我有一个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所以,基于以上数据我会洗牌/设置数组.

ash*_*cse 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);
Run Code Online (Sandbox Code Playgroud)

手动排序的方法如下:

从文件中读取并添加数据 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;
}
Run Code Online (Sandbox Code Playgroud)

你的主要方法是:

   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();
    }
Run Code Online (Sandbox Code Playgroud)


use*_*681 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;
                    }
                }
            });
Run Code Online (Sandbox Code Playgroud)