I have two arraylist filelist
and imgList
which related to each other, e.g. "H1.txt" related to "e1.jpg". How to automatically randomized the list of imgList
according to the randomization of fileList
? Like in excel, if we sort certain column, the other column will automatically follow?
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 e.g.:
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"};
This can be done using the shuffle method:
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. Loop over it to get the order for your shuffled file/image lists.
ArrayList newFileList = new ArrayList(); ArrayList newImgList = new ArrayList(); for ( 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;
Wrap them in another class so that you can end up with a single array or
List
of those objects.Usage example: