Java array shuffle vis Collections.shuffle

Description

Java array shuffle vis Collections.shuffle


import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {

  public static void main(String[] args) {
    String cardName[] = { "CSS", "Java", "Javascript", "HTML", "SQL" };

    cardName = shuffle(cardName);/*  w w  w .  j  a v  a2  s  .  c om*/
    System.out.println(Arrays.toString(cardName));

    cardName = shuffle(cardName);
    System.out.println(Arrays.toString(cardName));

    cardName = shuffle(cardName);
    System.out.println(Arrays.toString(cardName));

    cardName = shuffle(cardName);
    System.out.println(Arrays.toString(cardName));

  }

  public static String[] shuffle(String cardName[]) {
    List<String> cardList = Arrays.asList(cardName);
    Collections.shuffle(cardList);
    return (String[]) cardList.toArray(new String[cardName.length]);
  }
}



PreviousNext

Related