ArrayList Reverse Search Swap Shuffle Fill Java

Note: Before going through this program ArrayList Reverse Search Swap Shuffle Fill Java, it is advised to read first ArrayList Introduction where all the methods, class signature, constructors and history are given.

4 programs are given on Arraylist.

  1. ArrayList Methods: In this program, the basic operations on array list are performed like addition, checking the list is empty or not, removing the elements, knowing the index number of an element etc.
  2. ArrayList Iterators: In this example, different styles of retrieving and printing the array list elements are described.
  3. ArrayList Operations: In this program, operations like reversing, sorting, searching, swapping, shuffling, filling, knowing maximum and minimum values are performed. Finally, an array of array lists is created and the elements are printed.
  4. ArrayList Special: In this program, operations like converting array list to array, array list to string, copying elements of one array into another, converting array to array list and finally cloning of an array list are performed.

This is the third of the above four – ArrayList Operations.

Example on ArrayList Reverse Search Swap Shuffle Fill Java
import java.util.*;
public class ArrayListOperations
  public static void main(String args[])
    ArrayList al1 = new ArrayList();

    System.out.println("Max of al1: " + Collections.max(al1));
    System.out.println("Min of al1: " + Collections.min(al1));
    System.out.println("Index of 30: " + Collections.binarySearch(al1, 30));

    System.out.println("\nOriginal elements: " + al1); 
    Collections.swap(al1, 1, 3);
    System.out.println("Elements after swap 1 and 3: " + al1); 

    System.out.println("Reversed elements: " + al1); 
    System.out.println("\nFirst time shuffled elements: " + al1); 
    System.out.println("Second time shuffled elements: " + al1); 

    ArrayList al2 = new ArrayList();
    System.out.println("\nal2 elements before filling with 100: " + al2); 
    Collections.fill(al2, 100);
    System.out.println("al2 elements filled with 100: " + al2); 

    TreeSet ts = new TreeSet(al1);
    System.out.println("\nSorted elements: " + ts); 

    ArrayList myList[] = { al1, al2 };
    System.out.println("\nmyList[0] elements: " + myList[0]); 
    System.out.println("myList[1] elements: " + myList[1]); 

ArrayList Reverse Search Swap Shuffle Fill
Output screen of ArrayList Reverse Search Swap Shuffle Fill Java

ArrayList al1 = new ArrayList();

A generics ArrayList object al1 is created that stores only integer values. Five elements are added with add() method inherited from Collection interface.

System.out.println(“\nMax of al1: ” + Collections.max(al1));
System.out.println(“Min of al1: ” + Collections.min(al1));
System.out.println(“Index of 30: ” + Collections.binarySearch(al1, 30));

Collections methods like max() and min() are used to find the maximum value and minimum value of the array list elements. binarySearch() method is used to search for a particular element in the array list. It returns the index number of the element.

Collections.swap(al1, 1, 3);

swap() method swaps the elements 1 and 3. In the first element place third comes and in third element place first comes. Observe the screenshot. reverse() method reverses the existing elements in the list al1. Observe, the swapped elements are reversed.


shuffle() method of Collections class shuffles the elements randomly. Calling the same shuffle() method multiple times prints the elements in different order.

Collections.fill(al2, 100);

The fill() method replaces all the existing elements of al2 with 100. After this method call, all elements become 100.

TreeSet ts = new TreeSet(al1);

The subclasses of Set interface prints the elements in ascending order by default. To print the elements of array list al1 in ascending order (instead of writing a sort algorithm), al1 object is passed to TreeSet ts constructor. Now ts prints the elements in natural (ascending) order implicitly.

ArrayList myList[] = { al1, al2 };
System.out.println(“\nmyList[0] elements: ” + myList[0]);
System.out.println(“myList[1] elements: ” + myList[1]);

An array of ArrayList, myList, is created with the elements of array lists al1 and al2. myList[0] represents al1 and myList[1] represents al2.

2 thoughts on “ArrayList Reverse Search Swap Shuffle Fill Java”

  1. Sir,
    We create two array list objects one object contains ten records and another object contain hundred records both objects contains some matched records
    How can i delete the matched records with the help of equals()

Leave a Comment

Your email address will not be published.