package org.kodejava.example.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ArrayShuffle {
public static void main(String[] args) {
// Initialize the contents of our array
String[] alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
// As the Collections.shuffle() method need a list for the parameter
// we convert our array into List using the Arrays class.
List<String> list = Arrays.asList(alphabets);
// Here we just simply used the shuffle method of Collections class
// to shuffle out defined array.
Collections.shuffle(list);
// Run the code again and again, then you'll see how simple we do
// shuffling
for (String alpha : list) {
System.out.print(alpha + " ");
}
}
}
An example of the generated results are:
F H E A B I G J D C
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019