ArrayList Reverse Search Swap Shuffle Fill Java

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


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);
Collections.reverse(al1);

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.

Collections.shuffle(al1);
Collections.shuffle(al1);

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. Arun

    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 Reply

Your email address will not be published. Required fields are marked *