Java Program to shuffle an array using list


Let us first create a string array and convert it to a list. After that, the shuffle would be performed.

Following is the string array −

String str[] = {"A", "B", "C", "D", "E"};

Convert the string array to list −

List<String>list = Arrays.asList(str);

Now shuffle the array −

Collections.shuffle(list);

Example

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      String str[] = {"A", "B", "C", "D", "E"};
      List<String>list = Arrays.asList(str);
      Collections.shuffle(list);
      System.out.println("Shuffled the array using List = "+list.toString());
   }
}

Output

Shuffled the array using List = [C, B, E, D, A]

Updated on: 30-Jul-2019

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements