Shuffle an Integer Array In Java
This program can be used to shuffle an integer array using java. Basically user gives an integer array then this array is converted to an arraylist of integer type. Then that array list is passed to a predefined function shuffle() from the collection class !
Screenshot:
The Code
Got something to say? Comment them here !
Screenshot:
The Code
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Shuffle { public static void main(String args[]) { List<Integer> dataList = new ArrayList<Integer>(); Scanner s=new Scanner(System.in); int n[]=new int[4]; for(int i=0;i<4;i++) { n[i]=s.nextInt(); dataList.add(n[i]); } /*Alternate Method: * for(int i=0;i<4;i++) dataList.add(s.nextInt()); */ Collections.shuffle(dataList); int[] num = new int[dataList.size()]; for (int i = 0; i < dataList.size(); i++) { num[i] = dataList.get(i); } System.out.println("Shuffled Array is : "); for (int i = 0; i < num.length; i++) { System.out.println(num[i]); } } }
Got something to say? Comment them here !
Comments
Post a Comment