PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Java ArrayList

← All stacks

Theory

195/270

Java ArrayList

ArrayList is a resizable array that implements List. A normal array has a fixed size. ArrayList can grow when you add. Duplicates are allowed. Order is kept — first add stays first. This is the list you use in almost every fresher program.

Let’s take this on the board. ArrayList<String> names = new ArrayList<>(); names.add("Asha"); names.add("Ravi"); print names.size(); print names.get(0);. Output: 2 then Asha. Index from 0, same as array. get(1) is Ravi. get(2) throws IndexOutOfBoundsException — last valid index is size() - 1. Don’t write names.get(names.size()).

Fast get(i) is why we pick ArrayList day to day. Adding in the middle is slower because later items shift, like inserting a bench in the middle of a row. If you only add at the end and read by index, ArrayList is perfect. If you need unique names, use HashSet. If you insert/delete a lot at both ends, then think LinkedList — not on day one.

Cannot store raw int. ArrayList<Integer> marks = new ArrayList<>(); marks.add(90); works because of autoboxing (int → Integer). ArrayList<int> is illegal. Write the type in < >. Raw ArrayList without generics is old style and unsafe.

Diagram
ArrayList names
    add("Asha")  add("Ravi")
         │
         ▼
    [0]=Asha  [1]=Ravi   size=2
Exam tip

Say: resizable array, get(i) is fast. Write add + get + size. Mention Integer not int.

Example

import java.util.ArrayList;

// ArrayList demo
public class Main {
  public static void main(String[] args) {
    ArrayList<String> names = new ArrayList<>();
    names.add("Asha");
    names.add("Ravi");
    System.out.println(names.size());
    System.out.println(names.get(0));
  }
}

Java ArrayList — output: 2 then Asha. add Asha, add Ravi. size is 2. get(0) is Asha. get(2) would throw IndexOutOfBoundsException.

Short notes

  • DefArrayList = resizable array. List, ordered, duplicates OK.
  • Ruleadd, get(i), size. Fast index get. Middle insert is slower.
  • RememberList<String> names = new ArrayList<>(); No raw int — use Integer.
  • UseDaily growing list. Unique values → HashSet.

Questions

1

What is ArrayList?

2

Can it store int?

3

When not ArrayList?

Previous← Collections in JavaNextJava LinkedList →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.