105

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;
  }
}
Jonik
  • 80,077
  • 70
  • 264
  • 372
user2264733
  • 1,079
  • 2
  • 8
  • 5
  • 9
    Use [`Collections#shuffle(List list)`](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html). – skuntsel Apr 19 '13 at 19:54
  • http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm – Aurand Apr 19 '13 at 19:55
  • 2
    I don't see you using the "_random method_" anywhere at all. It looks like you haven't tried anything and have simply pasted your last assignment into the question box. Please put in some effort and only ask a question when you have something specific to ask. There are many questions on this site that explain how to randomize a list. – jahroy Apr 19 '13 at 19:57
  • Why would I put the random method in there if it didn't work? I obviously put some effort into if I have almost the whole assignment done, I just didn't know how to do this one part. – user2264733 Apr 19 '13 at 20:01
  • 1
    Whatever you say... But, there is no evidence in your code that you've attempted to solve the problem in question. Best of luck! Luckily for you, multiple people have told you exactly how to do what you want. Note there are also dozens of identical questions on this site. – jahroy Apr 19 '13 at 20:05
  • 2
    "Why would I put the random method in there if it didn't work?" So we can help you fix it, that's why. – thegrinner Apr 19 '13 at 20:07
  • 2
    "Why would I put the random method in there if it didn't work?" To demonstrate that you're **NOT** a lazy student who wants somebody to do his homework.. – jahroy Apr 19 '13 at 20:07
  • Please accept the answer which helped you most in solving your problem. It helps future readers. If the answers weren't helpful leave comments below them. So the poster can update them accordingly. Read [_What should I do when someone answers my question?_](https://stackoverflow.com/help/someone-answers) to know more. – Roshana Pitigala Mar 21 '19 at 03:58

2 Answers2

247

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.

Hamza Khan
  • 1,433
  • 13
  • 19
3urdoch
  • 7,192
  • 8
  • 42
  • 58
  • 1
    How do you then print out the result of that shuffled ArrayList? – Martin Erlic Sep 22 '16 at 07:54
  • 1
    @santafebound Arrays.toString(arrayList.toArray()); – Simon Baars Sep 28 '17 at 16:24
  • While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read [_How do I write a good answer_](https://stackoverflow.com/help/how-to-answer) to know more. – Roshana Pitigala Mar 21 '19 at 03:58
  • Below is an example of what Collections.shuffle(arrayList) does, and a way to print the result before and after shuffling: public static void main(String[] args) { ArrayList mylist = new ArrayList(); mylist.add("code"); mylist.add("quiz"); mylist.add("geeksforgeeks"); mylist.add("quiz"); mylist.add("practice"); mylist.add("qa"); System.out.println("Original List : " + mylist); Collections.shuffle(mylist); System.out.println("Shuffled List : " + mylist); } – Abir.d Apr 25 '19 at 09:05
34

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

AllTooSir
  • 48,828
  • 16
  • 130
  • 164