Home > Java > javaTutorial > body text

How to Randomize Multiple ArrayLists While Maintaining Ordering Consistency?

Barbara Streisand
Release: 2024-10-27 07:12:02
Original
531 people have browsed it

How to Randomize Multiple ArrayLists While Maintaining Ordering Consistency?

Maintaining Ordering Across Multiple ArrayLists during Randomization

Randomizing a single ArrayList is a straightforward task, but the challenge arises when multiple ArrayLists, related to each other, need to be randomized in a consistent fashion. To achieve this, we can leverage the power of Collections.shuffle() and a shared random seed.

The following code snippet demonstrates how to achieve the desired result:

<code class="java">import java.util.ArrayList;
import java.util.Collections;

public class ArrayListRandomization {

    public static void main(String[] args) {
        // Initialize the ArrayLists
        String[] file = {"H1.txt", "H2.txt", "H3.txt", "M4.txt", "M5.txt", "M6.txt"};
        ArrayList<String> fileList = new ArrayList<>(Arrays.asList(file));

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

        // Generate a shared random seed
        long seed = System.nanoTime();

        // Randomize the file list
        Collections.shuffle(fileList, new Random(seed));

        // Randomize the image list using the same seed
        Collections.shuffle(imgList, new Random(seed));

        // Print the randomized ArrayLists
        System.out.println("Randomized fileList:");
        for (String s : fileList) {
            System.out.println(s);
        }

        System.out.println("Randomized imgList:");
        for (String s : imgList) {
            System.out.println(s);
        }
    }
}</code>
Copy after login

In this code:

  • We obtain a shared random seed using System.nanoTime().
  • We use Collections.shuffle() with the shared seed to randomize the fileList.
  • We repeat the randomization process for the imgList using the same seed.

By using a shared random seed, both ArrayLists are randomized in the same way, ensuring that the relationship between the corresponding elements is maintained.

The above is the detailed content of How to Randomize Multiple ArrayLists While Maintaining Ordering Consistency?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!