Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

arraylist
java
java arraylist

What is an ArrayList in Java?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

An ArrayList class is a resizable array, which is present in the java.util package.

While built-in arrays have a fixed size, ArrayLists can change their size dynamically. Elements can be added and removed from an ArrayList whenever there is a need, helping the user with memory management.

ArrayLists are similar to vectors in C++:

Creating an ArrayList

Below is the syntax for declaring an ArrayList. Similar to arrays, it is required to specify the data type of elements in an ArrayList and it is up to the user to define its initial size.

import java.util.ArrayList; //import the ArrayList class
class MyClass {
public static void main( String args[] ) {
ArrayList<String> shapes = new ArrayList<String>(); // Create an ArrayList object with a string data type
}
}

Useful methods in ArrayList

Below are some useful methods in the ArrayList class:

Add an item: The add() method is used to add an item at the start of an ArrayList. The index of this item is 0 and all other indexes are increased by 1.

shapes.add("hexagon")

Access an item: The get() method, taking an index as input, is used to access an element in the ArrayList.

shapes.get(3)

Set an item: The set() method, taking an index as input, is used to set an element in the ArrayList at the specified index.

shapes.set(2, "triangle")

Remove an item: The remove() method, taking an index as input, is used to remove an element in an ArrayList. Indexes of all the elements in front of the removed element are reduced by 1.

shapes.remove(1)

Remove all items: The clear() method is used to remove all elements in an ArrayList.

shapes.clear()

Size of ArrayList: The size() method is used to find the number of elements in an ArrayList.

shapes.size()

ArrayList implementation

Below is a Java code for implementation of an ArrayList.

import java.util.ArrayList; // importing the ArrayList Class
class MyClass {
public static void main( String args[] ) {
ArrayList<String> shapes = new ArrayList<String>(); //create an ArrayList with string data type
shapes.add("square"); // add square at index 0
shapes.add("triangle"); // add triangle at index 1
shapes.add("hexagon"); // add hexagon at index 2
shapes.add("rhombus"); // add rhombus at index 3
shapes.add("octagon"); // add octagon at index 4
shapes.add("rectangle"); // add rectangle at index 5
shapes.add("pentagon"); // add pentagon at index 6
System.out.println(shapes); // prints all elements of ArrayList shapes
System.out.println(shapes.get(4)); // print element at index 4
shapes.remove(2); // removing element at index 2
shapes.remove(5); // removing element at index 5
System.out.println(shapes); // prints all elements of ArrayList shapes
shapes.set(4,"circle"); // replaces element at index 4 with circle
System.out.println(shapes); // prints all elements of ArrayList shapes
System.out.println(shapes.size()); // prints the number of elements in the ArrayList
shapes.clear(); // removes all elements in the ArrayList
System.out.println(shapes); // prints all elements of ArrayList shapes
}
}

RELATED TAGS

arraylist
java
java arraylist
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring