Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 3.4Arrays and Lists in Java

Collections

A collection is a structure that stores more than one piece of data. In Java, two of the most common types of collections are arrays and lists. Arrays have a fixed size, while lists can grow or shrink as needed.

Arrays

An array stores multiple values of the same data type together. Each value is stored in a numbered position called an index, starting from 0.

An array

An array with some values of type int where n is the index of the element.

Java
String[] words = { "Hello", "World" };
System.out.println(words[0]); // Output: Hello

You can also declare an array first, giving it a size, and then assign values later:

Java
String[] names = new String[2];
names[0] = "Hello";
names[1] = "World";

Using java.util.Arrays methods

Java provides a helper class called Arrays in the java.util package, which contains useful methods for sorting, searching, and modifying arrays.

Java
import java.util.Arrays;

String[] people = { "James", "Jack", "John", "Jamie", "Adam", "Frank" };

Arrays.sort(people);                 // Sort in alphabetical order
int position = Arrays.binarySearch(people, "Jamie"); // Search for "Jamie"
Arrays.fill(people, "?");            // Replace all values with "?"

Resizing an array

Arrays in Java are fixed-size, meaning their length cannot change after they are created. If you need to store more items, you must create a new, larger array and copy the existing values across.

Java
import java.util.Arrays;

int[] numbers = { 1, 2, 3 };
numbers = Arrays.copyOf(numbers, 4); // Create a new array of length 4
numbers[3] = 4;                         // Add new value

Although this is possible, it is inefficient for large amounts of data. When a collection needs to grow or shrink frequently, it is better to use a List.

Other useful array methods

  • Arrays.equals(array1, array2) – checks if two arrays are equal.
  • Arrays.copyOf(array, newLength) – copies an array to a new one.
  • Arrays.toString(array) – returns a string showing all values in an array.
  • Arrays.sort(array) – sorts the array in ascending order.
  • Arrays.binarySearch(array, value) – searches for a value in a sorted array.

Lists

A List is a more flexible collection that can grow and shrink as needed. The most common type is ArrayList, which works a lot like an array but with built-in methods for adding, inserting, removing, and clearing items.

Add

Java
import java.util.ArrayList;

ArrayList<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");

Insert

Java
list.add(0, "First"); // Insert at position 0

Remove

Java
list.remove("Hello"); // Removes first matching value

RemoveAt

Java
list.remove(1); // Removes value at index 1

Clear

Java
list.clear(); // Removes all values from the list

Convert a list to an array

Java
String[] asArray = list.toArray(new String[0]);

Other useful list methods

  • list.size() – returns the number of items in the list.
  • list.contains(value) – checks whether a value exists in the list.
  • list.get(index) – gets the item at a specific position.
  • list.set(index, value) – replaces the item at a specific position.
  • list.indexOf(value) – finds the position of a given value.
Feedback 👍
Comments are sent via email to me.