Program to display ArrayList and its size:
- add() method is used to append element to the list.
- size() method returns the length of list.
import java.util.*; class Code { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); System.out.println(“List is : ” + list); System.out.println(“Size is : ” + list.size()); } } |
Program to check the list is empty or not:
- isEmpty() method returns true if the list doesn’t contains elements else returns false
import java.util.*; class Code { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); if(list.isEmpty()) System.out.println(“List is empty”); else System.out.println(“List contains elements”); } } |
Program to display the element of specified index:
- get(int index) returns the element of specified index.
import java.util.*; class Code { public static void main(String[] args){ Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); for (int i=10 ; i<=50 ; i+=10){ list.add(i); } System.out.println(“List is : ” + list); System.out.println(“Enter index to display value : “); int loc = sc.nextInt(); System.out.println(“Element @ index-” + loc + ” is : ” + list.get(loc)); } } |
We specify the error message – if the index value is not present:
if(loc>=0 && loc<=list.size()-1) { System.out.println(list.get(loc)); } else { System.out.println(“Invalid index”); } | try { System.out.println(list.get(loc)); } catch(IndexOutOfBoundsException e) { System.out.println(“Invalid index”); } |
Insert element into specified index: add(int index, E e) method is used to insert element into specified index.
Instructions to code:
- Create ArrayList with 5 elements 10, 20, 30, 40, 50
- Read index to insert.
- Check whether the index is present or not
- If the index is present, then read the value and insert.
- If the index is not present, display Error message.
import java.util.*; class Code { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); System.out.println(“List is : ” + list); System.out.print(“Enter index to insert : “); int loc = sc.nextInt(); if(loc>=0 && loc<list.size()){ System.out.print(“Enter element to insert : “); int ele = sc.nextInt(); list.add(loc, ele); System.out.println(“List is : ” + list); } else{ System.out.println(“Invalid index”); } } } |
Program to remove all elements from the list: clear() method removes all elements from the list.
Instructions to code:
- Create list with 5 elements.
- Display – List is not empty
- Remove all elements using clear() method
- Display – List is empty.
import java.util.*; class Code { public static void main(String[] args){ List<Integer> list = new ArrayList<Integer>(); for(int i=1 ; i<=5 ; i++) list.add(i); System.out.println(“List is : ” + list); if(list.isEmpty()) System.out.println(“List is empty”); else System.out.println(“List is not empty”); list.clear(); System.out.println(“List is : ” + list); if(list.isEmpty()) System.out.println(“List is empty”); else System.out.println(“List is not empty”); } } |
Program to remove index element: remove(int index) method removes element of specified index.
Instructions to code:
- Create list with elements
- Read index value.
- If the index is valid – remove the element and display list
- If the index is not valid – display error message.
import java.util.*; class Code { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); for(int i=1 ; i<=5 ; i++){ list.add(i); } System.out.println(“List is : ” + list); System.out.print(“Enter index to remove : “); int loc = sc.nextInt(); if(loc>=0 && loc<list.size()){ list.remove(loc); System.out.println(“List is : ” + list); } else{ System.out.println(“Error : No such index to remove”); } } } |
Program to check whether the list contains element or not: contains() method returns true if the list has specified element.
import java.util.*; class Code { public static void main(String[] args){ Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); for(int i=1 ; i<=5 ; i++) list.add(i); System.out.println(“List is : ” + list); System.out.print(“Enter element to check in list : “); int ele = sc.nextInt(); if(list.contains(ele)) System.out.println(“Yes element is present in list”); else System.out.println(“No such element in list”); } } |
Program display the index value of element: indexOf() method returns index of specified element. It returns -1 if no such element in the list.
import java.util.*; class Code { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); for(int i=1 ; i<=5 ; i++) list.add(i); System.out.println(“List is : ” + list); System.out.print(“Enter element to find index value : “); int ele = sc.nextInt(); int index = list.indexOf(ele); if(index!=-1) System.out.println(“Index value is : ” + index); else System.out.println(“No such element in list”); } } |
Program to replace the existing value: set(int index, E e) method replace the index element with specified element.
Instructions to code:
- Create ArrayList with elements.
- Read the element to replace
- Check the element is present or not in the list using contains() method.
- If the element is present,
- Read the new element to replace with.
- If the element is not present,
- Display error message.
import java.util.*; class Code { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); for(int i=1 ; i<=5 ; i++){ list.add(i); } System.out.println(“List is : ” + list); System.out.print(“Enter element to replace : “); int x = sc.nextInt(); if(list.contains(x)) { System.out.print(“Enter new element : “); int y = sc.nextInt(); int loc = list.indexOf(x); list.set(loc, y); System.out.println(“Updated list : ” + list); } else System.out.println(“No such element in list”); } } |