Can we do the order of ArrayList randomly?

Asked 2 years ago, Updated 2 years ago, 142 views

There are two ArrayLists, filelist and imgList. If each of these is "H1.txt", it's related to "e1.jpg". When I change the order of the imgList automatically, I want to change the order according to the fileList. For example, when you sort rows in an Excel file, all the columns in that row are sorted together.

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);

When you change the order like this,

fileList = {"M4.txt","M6.txt","H3.txt","M5.txt","H2.txt","H1.txt"};
 imgList = {"e4.jpg","e6.jpg","e3.jpg","e5.jpg","e2.jpg","e1.jpg"};

I hope we get this result.

arraylist list collections java

2022-09-21 21:14

1 Answers

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

You can do it like this. Collection.You can put the same seed in Random when you use Shuffle.


2022-09-21 21:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.