Scientech Easy Programs on ArrayList in Java for Practice | Scientech Easy

Friday, November 2, 2018

Programs on ArrayList in Java for Practice

In this tutorial, we will learn the various type of programs on ArrayList in Java in an easy way and step by step. We will try to explain the reason for the output in an easy way so that you do not get any problem to clear the concept of ArrayList. So let's start.

Programs based on add, add an element at a specific index and addAll methods

Program source code 1:
    package com.arraylisttest; import java.util.ArrayList; import java.util.List; public class ArrayListExample1 { public static void main(String[] args) { // Creates an ArrayList with an initial capacity of 10. List<String> list=new ArrayList<String>(); // Call add() method to add elements at the end of the list using the reference variable list. System.out.println("Adding elements to end of list"); list.add("A"); list.add("B"); list.add("D"); list.add("E"); list.add("G"); System.out.println("ArrayList insertion order: " +list); System.out.println("Adding an element at a specific index after B element."); list.add(2, "C"); System.out.println("ArrayList insertion order after adding C: " +list ); System.out.println("Adding an element at a specific index after E"); list.add(5, "F"); System.out.println("ArrayList insertion order after adding F: " +list); } }
    Output: Adding elements to the end of the list ArrayList insertion order: [A, B, D, E, G] Adding an element at a specific index after B element. ArrayList insertion order after adding C: [A, B, C, D, E, G] Adding an element at a specific index after E ArrayList insertion order after adding F: [A, B, C, D, E, F, G]
Programs on ArrayList in Java for practice, ArrayList programs in Java with example.
A conceptual view of adding elements in ArrayList

In this example program, When we added a new element 'C' at a specific index 2, the element D,  E, and G will be shifted to the one position left. After inserting the C element, the reference variable 'list' will reassign to the new array list and copy all the elements into the new array list. The old default array list will be gone to the garbage collection. Now the position of the element E is shifted from position 3 to position 4 in the new array list. Therefore, the element F after E will add at position 5, not at position 4. See the above picture.

Program source code 2:
    package com.arraylisttest; import java.util.ArrayList; public class ArrayListExample2 { public static void main(String[] args) { // Creates an ArrayList1 with an initial capacity of 10. ArrayList<String> al=new ArrayList<String>(); al.add("G"); al.add("H"); al.add("I"); al.add("M"); al.add("N"); System.out.println("Original ArrayList insertion order: " +al); // Creates another ArrayList2 with an initial capacity of 10. ArrayList<String> al1=new ArrayList<String>(); al1.add("J"); al1.add("K"); al1.add("L"); // Call addAll() method to add all elements in the list1 at spcefic index 3. al.addAll(3, al1); System.out.println("ArrayList insertion order after adding group of elements in the list1 : " +al); // Creates ArrayList3 with initial capacity 10. ArrayList<String> al3=new ArrayList<String>(); al3.add("20"); al3.add("40"); al3.add("12"); // Adding group of elements at the end of list1. al.addAll(al3); System.out.println(al); } }
    Output: Original ArrayList insertion order: [G, H, I, M, N] ArrayList insertion order after adding a group of elements in the list1 : [G, H, I, J, K, L, M, N] [G, H, I, J, K, L, M, N, 20, 40, 12]

Programs based on remove, get, contains, and set methods

Program source code 3:
    package com.arraylisttest; import java.util.ArrayList; public class ArrayListExample3 { public static void main(String[] args) { // Creates an ArrayList with an initial capacity of 10. ArrayList<String> al=new ArrayList<String>(); al.add("Science"); al.add("Maths"); al.add("Hindi"); al.add("English"); al.add("Social Science"); System.out.println("Original ArrayList insertion order: " +al); // Call get() method to get element at the index 2. String str= al.get(2); // Since the return type of get method is String. So we will store it using str variable with data type string. System.out.println("Element at index 2: " +str); // Call contains() method to check whether the element English is present in the list or not. boolean b=al.contains("English"); // Since the return type of contains method is boolean. So we will store it by using 'b' variable with type boolean. // It returns true if the specified element is present in the list otherwise false. System.out.println(b); // return true. boolean bl=al.contains("Sanskrit"); System.out.println(bl); //return false. boolean bo=al.containsAll(al); // It will return truse if this collection contains all elements in the specified collection. System.out.println(bo); // call remove() method element English at a specified index. al.remove(3); System.out.println("After removing element, ArrayList Order: " +al); // Call set() method to replace element Social Science. al.set(3, "Computer"); System.out.println("After replacing element, ArrayList Order: " +al); }}
    Output: Original ArrayList insertion order: [Science, Maths, Hindi, English, Social Science] Element at index 2: Hindi true false true After removing element, ArrayList Order: [Science, Maths, Hindi, Social Science] After replacing element, ArrayList Order: [Science, Maths, Hindi, Computer]

Program based on Iteration of ArrayList using Iterator, ListIterator, Enumeration, & Enhanced for loop

Program source code 4:
    package com.arraylisttest; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class ArrayListExample4 { public static void main(String[] args) { // Creates an ArrayList object with an initial capacity of 10. List<Integer> list=new ArrayList<Integer>(); // Adding elements. list.add(20); list.add(25); list.add(30); list.add(35); list.add(40); System.out.println("Original ArrayList: " +list); System.out.println("--Using Iterator--"); // Call iterator() method to iterate over the elements. Iterator<Integer> itr=list.iterator(); // Checking the next element availability using reference variable itr. while(itr.hasNext()){ // Moving the cursor to next element using reference variable itr. Integer it=itr.next(); // Here return type is an integer. System.out.println(it); } System.out.println("--Iterating in forwarding direction using ListIterator--"); // Call ListIterator() method to iterate over the elements. ListIterator<Integer> litr=list.listIterator(); // Checking the next element availability using reference variable litr. while(litr.hasNext()){ Integer lit=litr.next(); System.out.println(lit); } System.out.println("--Using Enumeration-- "); Enumeration<Integer> enumlist=Collections.enumeration(list); while(enumlist.hasMoreElements()){ System.out.println(enumlist.nextElement()); } System.out.println("--Using Enhanced for loop--"); for(Integer in : list){ System.out.println(in); } } } }
    Output: Original ArrayList: [20, 25, 30, 35, 40] --Using Iterator-- 20 25 30 35 40 --Iterating in forwarding direction using ListIterator-- 20 25 30 35 40 --Using Enumeration-- 20 25 30 35 40 --Using Enhanced for loop-- 20 25 30 35 40

Program based on isEmpty, size, & clear methods

Program source code 5:
    package com.arraylisttest; import java.util.ArrayList; import java.util.List; public class ArrayListExample5 { public static void main(String[] args) { // Creates an array with an initial capacity of 10. List<String> list=new ArrayList<String>(); // Call isEmpty() method to check the elements in the list. boolean b=list.isEmpty(); // It will return true if this list contains no elements. System.out.println(b); // Adding elements to the end of the list. list.add("INDIA"); list.add("USA"); list.add("SRILANKA"); list.add("JAPAN"); list.add("FRANCE"); System.out.println("Original ArrayList insertion order: " +list); // Call size() method to get the number of elements or length of ArrayList in the list. Integer sizelist=list.size(); System.out.println("Size/Length of the list: " +sizelist); System.out.println(list.isEmpty()); // Return false. System.out.println("--Clear all elements from the list--"); list.clear(); boolean bol=list.isEmpty(); System.out.println(bol); Integer size=list.size(); System.out.println("No of elements in the list: " +size); } }
    Output: true Original ArrayList insertion order: [INDIA, USA, SRILANKA, JAPAN, FRANCE] Size/Length of the list: 5 false --Clear all elements from the list-- true No of elements in the list: 0

Program based on getting the index of the first occurrence of the element in the ArrayList

Program source code 6:
    package com.arraylisttest; import java.util.ArrayList; public class ArrayListExample6 { public static void main(String[] args) { // Creates an array list with an initial capacity of 10. ArrayList<String> al=new ArrayList<String>(); // Adding elements in the list. al.add("ABC"); al.add("DEF"); al.add("ABC"); // Duplicate elements are allowed in ArrayList. al.add("GHI"); al.add(null); // null element is allowed in the ArrayList System.out.println("Original ArrayList order:" +al); // Call indexOf() method to get the index of the first occurrence of a specific element in the list. Integer indexofElement=al.indexOf("ABC"); // It will return the index of the first occurrence of the element. System.out.println("Index of the element ABC: " +indexofElement); Integer indexElement=al.indexOf(null); System.out.println("Index of the element null: " +indexElement); Integer indexE=al.indexOf("JKL"); // It will return -1 if the element is not present in the list. System.out.println("Index of the element JKL: " +indexE); } }
    Output: Original ArrayList order:[ABC, DEF, ABC, GHI, null] Index of the element ABC: 0 Index of the element null: 4 Index of the element JKL: -1

Program based on getting the index of the last occurrence of the element in the ArrayList

Program source code 7:
    package com.arraylisttest; import java.util.ArrayList; public class ArrayListExample7 { public static void main(String[] args) { ArrayList<Integer> arlist=new ArrayList<Integer>(); arlist.add(20); arlist.add(25); arlist.add(30); arlist.add(35); arlist.add(40); arlist.add(25); arlist.add(20); System.out.println("Original ArrayList Order: " +arlist); Integer lastindex=arlist.lastIndexOf(25); System.out.println("Index of last occurrence of the element 25: " +lastindex); Integer lindex=arlist.lastIndexOf(20); System.out.println("Index of the element 20: " +lindex ); } }
    Output: Original ArrayList Order: [20, 25, 30, 35, 40, 25, 20] Index of last occurrence of the element 25: 5 Index of the element 20: 6
Final words:
We hope that seven programs on ArrayList in Java will help you to clear more concepts on ArrayList methods. All programs are very important for beginners. You follow all the steps with reasons to make good command on ArrayList concepts.

Popular