[java] How to shuffle an ArrayList

I need some help in writing a method that will shuffle the ArrayList. I can't figure out what to place in my method. Here is what I have so far. I tried using the random method to randomize the integers in the list but that didn't work. Can someone show me how to do this?

Here is the code I've tried:

import java.util.ArrayList;
import java.util.Scanner;

public class Lab 11 {
  public static void main(String[] args) {
    ArrayList<Double> list = new ArrayList<Double>();

    Scanner input = new Scanner(System.in);   
    System.out.print("Enter integers (input ends with 0): ");
     double value;

    do {
      value = input.nextDouble(); // Read a value from the input

      if (value != 0) 
        list.add(value); // Add the value if it is not in the list
    } while (value != 0);
     System.out.println("The maximum number is " + max(list));

     System.out.print("Enter five double values: ");
     for (int i = 0; i < 5; i++)
      list.add(input.nextDouble());

    System.out.println("The sum is " + sum(list));

  }

  public static Double max(ArrayList<Double> list) {
    if (list == null || list.size() == 0)
      return null;

    double result = list.get(0);
    for (int i = 1; i < list.size(); i++)
      if (result < list.get(i))
        result = list.get(i);

    return result;
  }

  public static double sum(ArrayList<Double> list) {
    double sum = 0;
    for (int i = 0; i < list.size(); i++)
      sum += list.get(i);
    return sum;
  }
}

This question is related to java list collections

The answer is


Use this method and pass your array in parameter

Collections.shuffle(arrayList);

This method return void so it will not give you a new list but as we know that array is passed as a reference type in Java so it will shuffle your array and save shuffled values in it. That's why you don't need any return type.

You can now use arraylist which is shuffled.


Try Collections.shuffle(list).If usage of this method is barred for solving the problem, then one can look at the actual implementation.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to list

Convert List to Pandas Dataframe Column Python find elements in one list that are not in the other Sorting a list with stream.sorted() in Java Python Loop: List Index Out of Range How to combine two lists in R How do I multiply each element in a list by a number? Save a list to a .txt file The most efficient way to remove first N elements in a list? TypeError: list indices must be integers or slices, not str Parse JSON String into List<string>

Examples related to collections

Kotlin's List missing "add", "remove", Map missing "put", etc? How to unset (remove) a collection element after fetching it? How can I get a List from some class properties with Java 8 Stream? Java 8 stream map to list of keys sorted by values How to convert String into Hashmap in java How can I turn a List of Lists into a List in Java 8? MongoDB Show all contents from all collections Get nth character of a string in Swift programming language Java 8 Distinct by property Is there a typescript List<> and/or Map<> class/library?