Q:

(Shuffle ArrayList) Write the following method that shuffles an ArrayList: public static <E> void shuffle(ArrayList<E> list)

0

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

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (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_19_08 {
	/** Method shuffles an ArrayList */
	public static <E> void shuffle(ArrayList<E> list) {
		for (int i = 0; i < list.size(); i++) {
			int index = (int)(Math.random() * list.size());
			E temp = list.get(i);
			list.set(i, list.get(index));
			list.set(index, temp);
		}
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now