Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shuffle an ArrayList [duplicate]

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;   } } 
like image 598
user2264733 Avatar asked Apr 19 '13 19:04

user2264733


People also ask

How do you shuffle an ArrayList?

In order to shuffle elements of ArrayList with Java Collections, we use the Collections. shuffle() method. The java. util.

How do I shuffle two Arraylists at once?

The simplest approach is to encapsulate the two values together into a type which has both the image and the file. Then build an ArrayList of that and shuffle it. That improves encapsulation as well, giving you the property that you'll always have the same number of files as images automatically.

Does ArrayList in Java allow duplicates?

ArrayList allows duplicate values in its collection. On other hand duplicate elements are not allowed in Hashset. HashSet is completely based on object also it doesn't provide get() method.

Is duplicate elements are removed before insertion in ArrayList?

The Element must be in ascending order, No duplicate elements in ArrayList<Integer> insert method run in O(n) times. One can have duplicate elements in the list but not in the ArrayList. Duplicate elements are removed before insertion , it does not happen in the list.


2 Answers

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.

like image 72
3urdoch Avatar answered Sep 28 '22 07:09

3urdoch


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

like image 21
AllTooSir Avatar answered Sep 28 '22 07:09

AllTooSir