简体   繁体   English

如何以相同的方式随机化两个 ArrayList?

[英]How to randomize two ArrayLists in the same fashion?

I have two arraylist filelist and imgList which related to each other, eg "H1.txt" related to "e1.jpg".我有两个彼此相关的arraylist filelistimgList ,例如与“e1.jpg”相关的“H1.txt”。 How to automatically randomized the list of imgList according to the randomization of fileList ?如何根据fileList的随机化自动随机化imgList的列表? Like in excel, if we sort certain column, the other column will automatically follow?就像在excel中一样,如果我们对某一列进行排序,另一列会自动跟随吗?

String [] file = {"H1.txt","H2.txt","H3.txt","M4.txt","M5.txt","M6.txt"};
ArrayList<String> fileList = new ArrayList<String>(Arrays.asList(file));

String [] img = {"e1.jpg","e2.jpg","e3.jpg","e4.jpg","e5.jpg","e6.jpg"};
ArrayList<String> imgList = new ArrayList<String>(Arrays.asList(img));

//randomized files
Collections.shuffle(fileList);

output after randomization eg:随机化后的输出,例如:

fileList = {"M4.txt","M6.txt","H3.txt","M5.txt","H2.txt","H1.txt"};

intended output:预期输出:

 imgList = {"e4.jpg","e6.jpg","e3.jpg","e5.jpg","e2.jpg","e1.jpg"};

Use Collections.shuffle() twice, with two Random objects initialized with the same seed:使用Collections.shuffle()两次,用相同的种子初始化两个Random对象:

long seed = System.nanoTime();
Collections.shuffle(fileList, new Random(seed));
Collections.shuffle(imgList, new Random(seed));

Using two Random objects with the same seed ensures that both lists will be shuffled in exactly the same way.使用具有相同种子的两个Random对象可确保两个列表以完全相同的方式进行混洗。 This allows for two separate collections.这允许两个单独的集合。

Wrap them in another class so that you can end up with a single array or List of those objects.将它们包装在另一个类中,以便最终得到这些对象的单个数组或List

public class Data {
    private String txtFileName;
    private String imgFileName;

    // Add/generate c'tor, getter/setter, equals, hashCode and other boilerplate.
}

Usage example:用法示例:

List<Data> list = new ArrayList<Data>();
list.add(new Data("H1.txt", "e1.jpg"));
list.add(new Data("H2.txt", "e2.jpg"));
// ...

Collections.shuffle(list);

The simplest approach is to encapsulate the two values together into a type which has both the image and the file.最简单的方法是将这两个值一起封装到一个类型中,该类型具有图像和文件。 Then build an ArrayList of that and shuffle it.然后建立一个ArrayList这一点,将它洗。

That improves encapsulation as well, giving you the property that you'll always have the same number of files as images automatically.这也改进了封装,为您提供了始终自动拥有与图像相同数量的文件的属性。

An alternative if you really don't like that idea would be to write the shuffle code yourself (there are plenty of examples of a modified Fisher-Yates shuffle in Java, including several on Stack Overflow I suspect) and just operate on both lists at the same time.如果您真的不喜欢这个想法,另一种选择是自己编写 shuffle 代码(有很多在 Java 中修改过的Fisher-Yates shuffle的例子,包括我怀疑的 Stack Overflow 上的几个),然后只对两个列表进行操作同时。 But I'd strongly recommend going with the "improve encapsulation" approach.但我强烈建议采用“改进封装”方法。

You could do this with maps:你可以用地图做到这一点:

Map<String, String> fileToImg:
List<String> fileList = new ArrayList(fileToImg.keySet());
Collections.shuffle(fileList);
for(String item: fileList) {
    fileToImf.get(item);
}

This will iterate through the images in the random order.这将以随机顺序遍历图像。

This can be done using the shuffle method:这可以使用 shuffle 方法完成:

private List<Integer> getJumbledList() {
     List<Integer> myArrayList2 = new ArrayList<Integer>();
        myArrayList2.add(8);
        myArrayList2.add(4);
        myArrayList2.add(9);
        Collections.shuffle(myArrayList2);
        return myArrayList2;

不要有两个字符串数组,而是有一个包含两个字符串的自定义类数组。

You can create an array containing the numbers 0 to 5 and shuffle those.您可以创建一个包含数字 0 到 5 的数组并将它们打乱。 Then use the result as a mapping of "oldIndex -> newIndex" and apply this mapping to both your original arrays.然后将结果用作“oldIndex -> newIndex”的映射并将此映射应用于原始数组。

Not totally sure what you mean by "automatically" - you can create a container object that holds both objects:不完全确定“自动”是什么意思 - 您可以创建一个包含两个对象的容器对象:

public class FileImageHolder { String fileName;公共类 FileImageHolder { 字符串文件名; String imageName;字符串图像名称; //TODO: insert stuff here } //TODO: 在这里插入东西 }

And then put that in an array list and randomize that array list.然后将其放入数组列表中并随机化该数组列表。

Otherwise, you would need to keep track of where each element moved in one list, and move it in the other one as well.否则,您需要跟踪每个元素在一个列表中移动的位置,并将其也移动到另一个列表中。

Unless there's a way to retrieve the old index of the elements after they've been shuffled, I'd do it one of two ways:除非有一种方法可以在洗牌后检索元素的旧索引,否则我会采用以下两种方法之一:

A) Make another list multi_shuffler = [0, 1, 2, ... , file.size()] and shuffle it. A) 创建另一个列表 multi_shuffler = [0, 1, 2, ... , file.size()] 并对其进行洗牌。 Loop over it to get the order for your shuffled file/image lists.循环遍历它以获取您洗牌的文件/图像列表的顺序。

ArrayList newFileList = new ArrayList(); ArrayList newFileList = new ArrayList(); ArrayList newImgList = new ArrayList(); ArrayList newImgList = new ArrayList(); for ( i=0; i对于 ( i=0; i

or B) Make a StringWrapper class to hold the file/image names and combine the two lists you've already got into one: ArrayList combinedList;或 B) 创建一个 StringWrapper 类来保存文件/图像名称并将您已经获得的两个列表合并为一个:ArrayList combineList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM