DEV Community

Harini
Harini

Posted on

ArrayList in Java

ArrayList is one of the most commonly used classes in Java's collections framework.It provides a dynamic array that can grow and shrink automatically as elements are added or removed. Unlike traditional arrays, you do not need to specify the size in advance.

ArrayList is part of the java.util package and implements the list interface.

Why do we need ArrayList?

  • In Java,array have a fixed size.Once an array is created,its size cannot be changed.

Limitations of Arrays

  • Fixed size.
  • Insertion and deletion are difficult.
  • No built-in methods for data manipulation.

To overcome these limitations,Java provides ArrayList,which offers:

  • Dynamic resizing
  • Easy insertion and deletion
  • Built-in utility methods

Features of ArrayList
1.Dynamic size

  • ArrayList automatically increases or decreases its capacity based on the number of elements.

2.Maintains insertion order

  • Elements are stored in the same order in which they are inserted.

3.Allows duplicate elements

  • Multiple identical values can be stored.

4.Random access

  • Elements can be accessed directly using their index.

5.Stores objects

  • ArrayList stores objects rather than primitive datatypes.Wrapper classes are used for primitives.
int -> Integer
double -> Double
char -> Character
Enter fullscreen mode Exit fullscreen mode

Top comments (1)