This is my code and has some problem please fix it thank you

//This is my code. and has some problem. please fix it. thank you

import java.util.ArrayList;

import java.util.Arrays;

public class shuffleArray {

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

for (int i = 0; i < list.size() -1 ; i++){

   int index=(int)(Math.random() * list.size());

           int temp = (int) list.get(i);

   list.set(index, list.get(index));

   list.set(index, temp);

}

}

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();

for (int i = 0; i < 10; i++) {

list.add(i);

}

ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(list.add()));

java.util.Collections.shuffle(list1);

for (int i = 0; i < list1.size(); i++) {

System.out.print(list1.get(i) + \" \");

}

}

}

}

Solution

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class ShuffleArray {

   public static void shuffle(ArrayList<Integer> list) {
       for (int i = 0; i < list.size() - 1; i++) {
           int index = (int) (Math.random() * list.size());
           int temp = (int) list.get(i);
           list.set(index, list.get(index));
           list.set(index, temp);
       }
   }

   public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<Integer>();
       for (int i = 0; i < 10; i++) {
           list.add(i);
       }

       ArrayList<Integer> list1 = new ArrayList<Integer>(list);
       Collections.shuffle(list1);

       for (int i = 0; i < list1.size(); i++) {
           System.out.print(list1.get(i) + \" \");
       }
   }
}

----------------------------------------------

compilation array was in below line:

ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(list.add()));

if my understanding is right, you are trying to create a List \'list1\' and initialize with the contents of \'list\'.

for that we need to just pass the list object to the constructor.

//This is my code. and has some problem. please fix it. thank you import java.util.ArrayList; import java.util.Arrays; public class shuffleArray { public static
//This is my code. and has some problem. please fix it. thank you import java.util.ArrayList; import java.util.Arrays; public class shuffleArray { public static

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site