Array List In java

 What is an Array List?

An ArrayList in Java is a dynamic array-like data structure. where the size is fixed upon creation, an ArrayList can grow or shrink dynamically as elements are added or removed.

Advantage of ArrayList over Array?

  • Dynamic Sizing
  • Automatic Resizing:
  • Range of Methods(In-build Functions):

 Library of ArrayList?

import java.util.ArrayList;

Syntex:

Hard Code(Array Initialization):

List<String> fruits = new ArrayList<>(List.of("Apple", "Banana", "Orange"));

Soft Code(Array Decleration):

Array of Integer:  ArrayList<Integer> intList = new ArrayList<>();

Array of float:  ArrayList<Float> floatList = new ArrayList<>();

Array of Double:  ArrayList<Double> doubleList = new ArrayList<>();

Array of String:  ArrayList<String> stringList = new ArrayList<>();

Array of char: ArrayList<Character> charList = new ArrayList<>();

Methods Of ArrayList :

add(); //This method is used to add elements to the ArrayList.
set(); //This method is used to update an element at a specific index
remove(); //method is used to remove an element at a specific index.
contains(); //This method checks if an element is present in the
indexOf(); //This method returns the index of the element if found.

Class Array:

import java.util.ArrayList; 
public class ArrayListBasicOperations { public static void main(String[] args) {
 // Create an ArrayList of Strings 
ArrayList<String> names = new ArrayList<>(); 

// Add elements to the ArrayList 

names.add("Ali"); 
names.add("Sara"); 
names.add("Sobia"); 
names.add("Saima"); 
System.out.println("Initial ArrayList: " + names);

 // Update an element 

names.set(1, "Amna"); 
System.out.println("After updating: " + names);

 // Delete an element names.remove(2); 

System.out.println("After deletion: " + names);

 // Search for an element String

 searchName = "Sara"; 
if (names.contains(searchName)) { 
System.out.println(searchName + " found at index " + names.indexOf(searchName)); }
else { 
System.out.println(searchName + " not found in the ArrayList."); } 
} }

Practice Programs:

  1. ArrayList Initialization: a. Create an ArrayList of integers and add the numbers 5, 10, 15, and 20 to it. b.Create an ArrayList of strings and add the words "apple", "banana", "orange", and "grape" to it.
  2. Element Manipulation: a. Create an ArrayList of doubles and add the values 3.5, 7.2, 9.8, and 4.6 to it. b. Update the second element to have a value of 6.4. c. Remove the third element from the ArrayList.
  3. Searching and Accessing: a. Create an ArrayList of characters and add the letters 'a', 'b', 'c', 'd', and 'e' to it. b. Check if the ArrayList contains the letter 'c'. If yes, print its index. c. Access and print the element at index 2.
  4. ArrayList Iteration: a. Create an ArrayList of integers and add the numbers 1 through 5 to it. b. Use a for-each loop to iterate through the ArrayList and print each element.
  5. ArrayList Reversal: a. Create an ArrayList of strings and add "one", "two", "three", "four", and "five". b. Reverse the order of elements in the ArrayList and print the reversed list.
  6. ArrayList Merge: a. Create two ArrayLists of integers and add some numbers to each. b. Merge the two ArrayLists into a single ArrayList and print the combined list.
  7. ArrayList Sorting: a. Create an ArrayList of integers with values 25, 10, 45, 5, and 30. b. Sort the ArrayList in ascending order and print the sorted elements.
  8. ArrayList Filtering: a. Create an ArrayList of strings containing names of fruits and vegetables. b. Create a new ArrayList and copy only the names of fruits from the original list. c. Print the new ArrayList containing only fruit names.
  9. ArrayList Searching and Counting: a. Create an ArrayList of integers with values 5, 10, 5, 15, 20, and 5. b. Search for the value 5 in the ArrayList and print the indices where it's found.
  10. ArrayList Clearing: a. Create an ArrayList of strings with some elements. b. Clear all elements from the ArrayList and print its size.