Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList

Posted on July 22, 2025July 22, 2025 By Admin No Comments on Java ArrayList

Introduction to ArrayList in Java

ArrayList is one of the most used classes in the Java Collections Framework. It provides a dynamic array for storing elements, allowing both random access and flexible size management. Unlike regular arrays in Java, an ArrayList can automatically resize itself as elements are added or removed, making it ideal for scenarios where the number of items can change over time.

Features of Java ArrayList

  • Dynamic Resizing: No need to specify a fixed size up front.
  • Indexed Access: Retrieve elements in constant time using an index.
  • Allows Duplicates: Elements can be repeated.
  • Maintains Insertion Order: Elements keep their original order.
  • Works with Generics: Type-safe collections prevent runtime errors.
  • Implements List Interface: Can be used anywhere a List is expected.

Syntax: How to Create an ArrayList

import java.util.ArrayList;

ArrayList<Type> listName = new ArrayList<>();

Example for storing integers:

ArrayList<Integer> numbers = new ArrayList<>();

Example for storing custom objects:

ArrayList<Student> students = new ArrayList<>();

Common ArrayList Operations

OperationDescriptionExample Code
AddInsert element at the endlist.add(value);
Insert at indexInsert at specific positionlist.add(index, value);
Get elementRetrieve value by indexlist.get(index);
Set elementUpdate value at given indexlist.set(index, value);
Remove by indexDelete an element at positionlist.remove(index);
Remove by valueDelete the first matching objectlist.remove(value);
SizeGet number of elementslist.size();
ClearRemove all elementslist.clear();
ContainsCheck if value is presentlist.contains(value);

Example: Basic Usage of ArrayList

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        System.out.println(fruits); // Output: [Apple, Banana, Orange]
        
        fruits.remove("Banana");
        System.out.println(fruits.get(1)); // Output: Orange
        System.out.println(fruits.size()); // Output: 2
    }
}

Advantages of ArrayList

  • Flexible size compared to arrays.
  • Easy element addition/removal (except for the middle elements, where removal can be slower).
  • Built-in methods for searching, sorting, and updating elements.

Limitations & When to Avoid

  • Not thread-safe: For concurrent use, use Collections.synchronizedList() or CopyOnWriteArrayList.
  • Slower insertions/deletions in the middle of the list (shifting of elements required).
  • Not for primitive types: Use wrappers (Integer, Double, etc.) instead of primitives (int, double).

If you need frequent insertions/removals at both ends, consider using LinkedList. For frequent random access and dynamic resizing, ArrayList works best.

Java ArrayList vs. Array and LinkedList

FeatureArrayArrayListLinkedList
SizeFixedDynamicDynamic
Insertion/RemovalManual, limitedEasy, slow in middleEasy, fast in middle
Access by indexFast (O(1))Fast (O(1))Slow (O(n))
MemoryCompactSlight overheadExtra memory for links

Conclusion

ArrayList is a versatile, flexible, and strongly supported part of Java’s Collections Framework. It’s most appropriate when you want to store elements in a list with frequent random access and only occasional insertions or removals.

Java Tags:Java-ArrayList, Java-Collections

Post navigation

Previous Post: Java Collections Framework
Next Post: Java ArrayList Methods

Leave a Reply Cancel reply

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

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method

Recent Posts

  • Java ArrayList remove() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList iterator() Method
  • Java ArrayList isEmpty() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme