N
N
nikita_chiru2016-07-22 11:47:41
Java
nikita_chiru, 2016-07-22 11:47:41

How can I make random fragments in the list?

I have a list in which there will be 9 elements that should be randomly selected from the list of fragments ...
how can I do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-07-22
@nikita_chiru

List can be shuffled using Collections.shuffle

import java.util.*;

public class ShuffleDemo {
    public static void main(String[] args) {
        Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(numbers));

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

        for(int x = 0; x < numbersList.size(); x++) {
                System.out.print(numbersList.get(x) + " ");
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question