Home Java Example Java Util Shuffling the Element of a List or Array
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Shuffling the Element of a List or Array

Advertisement
In this section, you will learn about shuffling the element of a list or array.

Shuffling the Element of a List or Array

     

Shuffling is the technique i.e. used to randomize the list or array to prepare each and every element for the operation. In this section, you will learn about shuffling the element of a list or array. Shuffling the element means moving the element from one place to another place randomly.

In the given program, list is created which contains some values. List lists the elements of the Collection. This program takes some values for the list and it sorts these values after shuffling. For this purposes some methods and APIs are explained below which have been used in the program: 

List:
This is the class of java.util.*; package which extends Collection. This is used to list elements available in the Collections. In this program a argument <Integer> has been passed with the List for type checking.

ArrayList():
This is the construction of ArrayList class of the java.util.*; package that extends the AbstractList class. It constructs an empty list. You can specify the capacity of the list by passing a value to the constructor.

sort(list):
This is the method of the Collections class which is used to sort the list elements in the default order.

Here is the code of program:

import java.util.*;
import java.io.*;

public class ShufflingListAndArray{
  public static void main(String[] argsthrows IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("How many elements you want to add to the list: ");
  int n = Integer.parseInt(in.readLine());
  System.out.print("Enter " + n + " numbers to sort: ");
  List<Integer> list = new ArrayList<Integer>();
  for(int i = 0; i < n; i++){
  list.add(Integer.parseInt(in.readLine()));
  }
  Collections.shuffle(list);
  Collections.sort(list);
  System.out.println("List sorting :"+ list);
  }
}

Download this example

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Related Tutorials

Ask Questions?    Discuss: Shuffling the Element of a List or Array   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
sushant behera
April 25, 2011
some thing wrong you are writing

in util package you are writing .List: This is the class of java.util.*; package which extends Collection. This is used to list elements available in the Collections. In this program a argument <Integer> has been passed with the List for type checking. can you tell me list is a class or interface . if you don't know why you writing this type of wrong notion.
Abednigo Obadje
February 29, 2012
I need help,

I need to Write a Java program that shuffles the elements of an integer array. First, it should define a positive integer constant , prompt the user to enter  integer numbers, and store them in an array. Then, it should print out the array by calling an array printing method (e.g. static void printArray(int[] array)). The array printing method should format the array by including the â??[ â?? and the â??]â?? characters at the beginning and the end of the arrays, printing the array elements on the same line, separated by a â??,â?? (comma). After this, the array should be given to a method that randomly reorders or shuffle the elements in the array (e.g. static void shuffleArray(int[] array)). Finally, the reordered array should also be printed using the above array printing method.
chockalingam
March 28, 2012
regarding collections class

why we should do a shuffling first before the sort?
Majed2020
May 1, 2012
Write a program Java

Write a program to perform to following operations on an array of 20 integers: -Add an element on request. If an over follow has occurred then display appropriate message. -Delete a specific element from the array. In case of not found or underflow just display a message. -Search for any element using binary search method. - implement a sort method to be executed before the output operation - List the elements that are available in the array - Find the average of the positive numbers in the array. - Wire a menu that could help the user to select the needed operation.
DMCA.com