Back to Articles
Java8 min read

Arrays and ArrayLists in Java

A beginner-friendly walkthrough of Java arrays and ArrayLists, written for students who found arrays confusing at first.

By Jaimee Grubb

North Garland H.S. Chapter

When I was first learning to code I found arrays to be one of the most confusing parts of programming, so hopefully this can help new programmers understand it better. The code examples below are specifically for Java.

What are Arrays?

An array is a collection of values that have a fixed size, like a list of ingredients. Specifically for Java, arrays can only consist of one data type. So if you wanted to make an array that has all your friends' names in it, then the data type would need to be a string.

How to Create an Array

There are multiple ways to create an array. It doesn't matter which way you create the array. There are specific situations where one approach may be better than the other, but it is up to your preference.

Example 1

int[] ages = {12, 20, 16, 32};
  • int — data type
  • [] — brackets show that we are creating an array
  • ages — array name
  • {12, 20, 16, 32} — the elements stored in the array
String[] names = {"Sarah", "Jerry", "Lilian"};

Example 2

String[] names = new String[5];
  • String — data type
  • [] — brackets show that we are creating an array
  • names — array name
  • new String[5] — create a new string array with the size of 5 (the 5 elements will be null / 0 / false / 0.0)

To store a value in the array created, use:

names[0] = "Sarah";
names[4] = "Jerry";

This is how to store a value at a specific place in an array. The first element (index) in an array is at 0, not 1. So in this example the last value would be at 4 instead of 5.

Ways to use an Array

You can print an array, find the length of an array, find the value at an index, or set a value at an index (which was seen above).

Given the following array for the examples below:

int[] array = {12, 20, 16, 32};

Print an array

System.out.println(array);

Print a value at a certain index

System.out.println(array[0]);

Find the length of an array

System.out.println(array.length);

Can you Add or Remove Elements in an Array?

No. If you wanted to add or remove elements in a list, then you should use an ArrayList instead. Because arrays have fixed sizes, you can't add an element at an index higher than the set size. ArrayLists, on the other hand, don't have a set size, so elements can be added or removed.

How to create an ArrayList

To use ArrayLists you need to import the package java.util.ArrayList at the beginning of your code.

ArrayList<String> names = new ArrayList<String>();
names.add("Kenzie");
names.add("Ayden");
  • ArrayList<String> — ArrayList object of a string data type
  • names — the name of the ArrayList
  • new ArrayList<String>() — create a new object of the ArrayList
  • add — adds an element to the end of the ArrayList

ArrayLists use the wrapper class to define the data types.

ArrayList<Integer> list = new ArrayList<Integer>();
Data typeWrapper Class
intInteger
doubleDouble
booleanBoolean
charCharacter
floatFloat

Ways to use an ArrayList

Uses the ArrayList in the above example.

Get an element at a specific index

names.get(0);

Find the length of the ArrayList

names.size();

Changing an element

names.set(1, "Olivia");

Removing an element

names.remove(0);

Remove all the elements

names.clear();

Citations