Java ArrayList shuffle with your own method

Description

Java ArrayList shuffle with your own method

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // generate ordered list of Number objects
    ArrayList<Number> list = new ArrayList<>();
    for (int i = 0; i < 15; i++) {
      list.add(i);/* www .j  a  v a  2s  . c o  m*/
    }

    displayList(list);

    // shuffle the list
    shuffle(list);

    displayList(list);

  }

  public static void shuffle(ArrayList<Number> list) {
    for (int i = 0; i < list.size(); i++) {
      int randomIndex = (int)(Math.random() * list.size());
      Number temp = list.get(i);
      list.set(i, list.get(randomIndex));
      list.set(randomIndex, temp);
    }
  }

  public static void displayList(ArrayList<Number> list) {
    for (int i = 0; i < list.size(); i++) {
      System.out.print(list.get(i) + " ");
    }
    System.out.println();
  }
}



PreviousNext

Related