1.3 Java Arrays Explained Simply


22/05/25

6 mins

Java Arrays Explained Simply: From Basics to Real-World Use

Arrays are one of the most important building blocks in Java programming. Whether you want to store a list of numbers or manage collections of names, arrays make it possible to organize and access data efficiently. In this beginner-friendly guide, we’ll walk you through the fundamentals of arrays using clear explanations and real examples based on Java’s official documentation at dev.java.

What Is an Array?

An array in Java is a container that holds a fixed number of values of the same type. Once an array is created, its length (the number of values it can hold) doesn’t change.

Think of it like a row of boxes where each box can store one item—like 10 integers or 5 names. Each "box" is called an element, and each has a numerical index, starting from 0.

How to Declare and Create an Array

Step 1: Declare

To declare an array, you specify the element type followed by square brackets [] and then the name of the array.

int[] anArray; // preferred form

You can also write:

int anArray[]; // allowed but discouraged

Step 2: Create

To actually create the array (i.e., allocate memory), use the new keyword:

anArray = new int[10]; // an array of 10 integers

Now anArray can hold 10 integers (from index 0 to 9), but they’re all set to 0 initially.

How to Initialize and Access Array Elements

You can assign values to individual elements using their index:

anArray[0] = 100; anArray[1] = 200; // and so on...

To access and print values:

System.out.println("Element at index 0: " + anArray[0]); System.out.println("Element at index 1: " + anArray[1]);

Shortcut for Declaring and Initializing Together

You can also declare and assign values all at once:

int[] anArray = {100, 200, 300, 400, 500};

The length is automatically set based on the number of elements.

Using Loops to Access Arrays

Instead of writing each line manually, use a loop:

for (int i = 0; i < anArray.length; i++) { System.out.println("Element at index " + i + ": " + anArray[i]); }

Multidimensional Arrays

In Java, you can create arrays of arrays—often called 2D arrays or multidimensional arrays.

Example:

String[][] names = { {"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"} }; System.out.println(names[0][0] + names[1][0]); // Mr. Smith System.out.println(names[0][2] + names[1][1]); // Ms. Jones

Each row in a 2D array can even have different lengths—this is known as a jagged array.

Using the Length Property

All arrays in Java have a built-in .length property:

System.out.println(anArray.length); // prints number of elements

It’s especially useful when working with loops or dynamic data.

Copying Arrays

To copy part of one array into another, Java provides built-in tools:

Using System.arraycopy

String[] copyFrom = {"A", "B", "C", "D", "E"}; String[] copyTo = new String[3]; System.arraycopy(copyFrom, 1, copyTo, 0, 3); // copyTo now contains {"B", "C", "D"}

Using java.util.Arrays.copyOfRange

String[] newCopy = java.util.Arrays.copyOfRange(copyFrom, 1, 4); // same result as above

Other Handy Array Utilities

Java includes utility methods for arrays in the java.util.Arrays class:

  • toString() – Convert array to readable string

    System.out.println(Arrays.toString(copyTo));
  • sort() – Sort the array

    Arrays.sort(copyTo);
  • binarySearch() – Search for an element efficiently

  • fill() – Fill array with a specific value

  • equals() – Compare contents of two arrays

  • stream() – Convert array to a stream for advanced operations

Final Thoughts

Arrays help you manage collections of data effectively. By learning how to declare, initialize, access, and manipulate arrays, you gain a key skill for building robust Java programs.

From simple number lists to multidimensional models, mastering arrays lays a strong foundation for everything that comes next.

To dive deeper and try out code examples, explore the full Java guide on Arrays at dev.java.

Happy coding!


PREVIOUS: 1.2 Getting Started with Java Primitive Types

NEXT: 1.4 Simplifying Java with var