Monday, 16 January 2017

Chapter 19 Exercise 8, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

19.8 (Shuffle ArrayList)
Write the following method that shuffles an ArrayList:

public static <E> void shuffle(ArrayList<E> list)


import java.util.ArrayList;

public class Exercise_08 {

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        System.out.println(list);
        shuffle(list);
        System.out.println(list);

    }

    public static <E> void shuffle(ArrayList<E> list) {

        for (int i = 0; i < list.size(); i++) {
            int random = (int) (Math.random() * list.size());
            E tmp = list.get(random);
            list.set(random, list.get(i));
            list.set(i, tmp);
        }
    }
}

No comments :

Post a Comment