Paramclasses Academy - Blog

Tutorials Details

What is Java Arrays : One-Dimensional and Multidimensional Arrays ?

What is Java Arrays : One-Dimensional and Multidimensional Arrays ?

October 21, 2026

Introduction

Arrays are an important concept in Java programming. When you need to store multiple values of the same data type, using a separate variable for every value can make your program difficult to manage.

For example, suppose you want to store the marks of five students. You could create five different variables, but Java arrays provide a simpler way to store and access these values.

In this tutorial, you will learn what arrays are, how to declare and initialize them, how to access array elements, and how to work with one-dimensional and multidimensional arrays using practical Java examples.

What Is an Array in Java?

An array in Java is an object that stores a fixed number of values of the same declared element type. Each value is called an element, and each element is accessed using an index.

Array indexing starts from 0, not 1.

For example:

int[] marks = {85, 90, 78, 92, 88};

This array stores five integer values.

  • marks[0] gives 85.

  • marks[1] gives 90.

  • marks[2] gives 78.

  • marks[3] gives 92.

  • marks[4] gives 88.

Important: Once an array is created, its length cannot be changed. If you need a dynamically growing collection, Java provides classes such as ArrayList.

Why Do We Use Arrays in Java?

Arrays are useful when you need to:

  • Store multiple values of the same type.

  • Organize data using a single variable.

  • Access values using indexes.

  • Process data with loops.

  • Perform operations such as calculating totals and averages.

  • Represent tables and grid-like data using multidimensional arrays.

How to Declare an Array in Java

You can declare an array using the following syntax:

dataType[] arrayName;

For example:

int[] numbers;
String[] names;
double[] prices;

These statements declare array variables. They do not create the actual arrays yet.

You can also write int numbers[], but int[] numbers is generally preferred because it makes the array type clearer.

How to Create and Initialize an Array

There are several ways to create and initialize arrays in Java.

1. Declare and initialize an array directly

int[] numbers = {10, 20, 30, 40, 50};

System.out.println(numbers[0]);
System.out.println(numbers[2]);

Output:

10
30

2. Create an array using the new keyword

int[] numbers = new int[5];

numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

Here, new int[5] creates an integer array with five elements. Integer elements are initially set to 0.

Other default values include false for boolean elements and null for reference-type elements.

Understanding Array Indexes

Each array element has an index representing its position.

Consider this example:

String[] fruits = {"Apple", "Mango", "Banana"};

System.out.println(fruits[0]);
System.out.println(fruits[1]);
System.out.println(fruits[2]);

Output:

Apple
Mango
Banana

The first element has index 0, and the last element has index array.length - 1.

Trying to access an invalid index, such as fruits[3], causes an ArrayIndexOutOfBoundsException at runtime.

What Is the Length of an Array?

Java arrays provide the length property to tell you how many elements they contain.

int[] numbers = {10, 20, 30, 40, 50};

System.out.println("Array length: " + numbers.length);

Output:

Array length: 5

Remember that length is a property, not a method. Write numbers.length, not numbers.length().

One-Dimensional Arrays in Java

A one-dimensional array stores elements in a single sequence.

For example:

public class Main {
    public static void main(String[] args) {
        int[] marks = {85, 90, 78, 92, 88};

        for (int i = 0; i < marks.length; i++) {
            System.out.println("Marks: " + marks[i]);
        }
    }
}

Output:

Marks: 85
Marks: 90
Marks: 78
Marks: 92
Marks: 88

The loop starts at index 0 and continues while i is smaller than the array length.

Using an Enhanced For Loop

Java also provides an enhanced for loop, often called a for-each loop.

int[] numbers = {10, 20, 30, 40};

for (int number : numbers) {
    System.out.println(number);
}

This loop visits each element without requiring you to manage an index manually. It is useful when you need to read every element in sequence.

Practical Example: Calculate the Sum and Average

Let's calculate the total and average marks stored in an array.

public class Main {
    public static void main(String[] args) {
        int[] marks = {80, 90, 75, 85, 70};

        int sum = 0;

        for (int mark : marks) {
            sum += mark;
        }

        double average = (double) sum / marks.length;

        System.out.println("Total marks: " + sum);
        System.out.println("Average marks: " + average);
    }
}

Output:

Total marks: 400
Average marks: 80.0

The program adds every element to sum. It then divides the total by the number of elements to calculate the average.

The cast (double) ensures that the division can produce a decimal result.

Multidimensional Arrays in Java

A multidimensional array is an array whose elements can themselves be arrays. The most common example is a two-dimensional array, which is useful for representing rows and columns.

For example, you can represent student marks in a table.

int[][] marks = {
    {80, 85, 90},
    {75, 88, 92},
    {89, 78, 84}
};

This is a two-dimensional array with three rows, each containing three elements.

You can access an element using two indexes:

System.out.println(marks[0][0]);
System.out.println(marks[1][2]);
System.out.println(marks[2][1]);

Output:

80
92
78

The first index identifies the row, and the second index identifies the element within that row.

How to Print a Two-Dimensional Array

You can use nested loops to visit each row and column.

public class Main {
    public static void main(String[] args) {
        int[][] numbers = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers[i].length; j++) {
                System.out.print(numbers[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output:

1 2 3
4 5 6
7 8 9

The outer loop visits each row. The inner loop visits every element in the current row.

Jagged Arrays in Java

Java also supports arrays in which each row has a different length. These are commonly called jagged arrays.

int[][] numbers = {
    {1, 2},
    {3, 4, 5},
    {6}
};

for (int i = 0; i < numbers.length; i++) {
    for (int j = 0; j < numbers[i].length; j++) {
        System.out.print(numbers[i][j] + " ");
    }
    System.out.println();
}

Each row is an individual array, so the number of elements in each row can differ.

One-Dimensional vs Multidimensional Arrays

Feature One-Dimensional Array Two-Dimensional Array
Structure A single sequence Rows and columns
Indexes One index Two indexes
Example int[] a int[][] a
Access a[0] a[0][1]
Common use A list of marks A table of marks

Common Mistakes When Using Arrays

1. Starting the index at 1

Array indexes begin at 0. The first element is array[0].

2. Accessing an invalid index

For an array of length five, valid indexes are 0 through 4.

3. Confusing length with length()

Use array.length for arrays. The length() method is used by strings.

4. Forgetting default values

Newly created arrays contain default values. For example, an int array starts with zero-valued elements.

5. Assuming arrays grow automatically

An array has a fixed length after creation. Use an ArrayList when you need a resizable list.

6. Assuming every two-dimensional array is a perfect rectangle

Java permits rows of different lengths, so use numbers[i].length when iterating over a row.

Frequently Asked Questions (FAQs)

1. What is an array in Java?

An array is an object that stores a fixed number of elements of the same declared type, with each element accessed by an index.

2. Why does array indexing start from zero?

Java uses zero-based indexing, so the first element is at index 0 and the last is at index length - 1.

3. Can we change the size of an array after creating it?

No. The length of an existing array cannot be changed. You can create a new array or use a resizable collection such as ArrayList.

4. What is the difference between array.length and String.length()?

array.length is a property of an array, whereas String.length() is a method that returns the number of characters in a string.

5. What is a multidimensional array?

It is an array whose elements are themselves arrays. A two-dimensional array is commonly used to represent rows and columns.

6. What happens when we access an invalid array index?

Java throws an ArrayIndexOutOfBoundsException at runtime.

Practice Exercises for Beginners

Try these exercises to improve your Java array skills:

  1. Create an integer array containing five numbers and print every element.

  2. Find the largest number in an array.

  3. Calculate the sum and average of an array of marks.

  4. Print an array in reverse order.

  5. Create a two-dimensional array and display its elements in rows and columns.

Try solving these exercises yourself before checking a solution.

Conclusion

Java arrays help programmers store and process multiple values efficiently. In this tutorial, you learned array declaration, initialization, indexing, the length property, one-dimensional arrays, two-dimensional arrays, jagged arrays, and practical examples using loops.

Understanding arrays will make it easier to learn more advanced Java concepts, including the Collections Framework and ArrayList.