Default Image

Months format

Show More Text

Load More

Related Posts Widget

Article Navigation

Contact Us Form

404

Sorry, the page you were looking for in this blog does not exist. Back Home

Different Types of Arrays in Java

Arrays are used to store multiple values of the same data type in a single variable. They allow efficient data management and quick access using index positions. Since arrays are objects in Java, they follow object-oriented principles and are stored in heap memory.

Different Types of Arrays in Java


Here, we will understand the different types of arrays available in Java and how they are used in real-world programming.


Single-Dimensional Array

A single-dimensional array in Java is the most basic type of array. It stores elements in a linear sequence, where each element can be accessed using a single index value. This type of array is commonly used when data needs to be stored in a simple list format.


Declaration

The single dimensional array is declared as follows:

int[] numbers;


Initialization

You can initialize the array with the elements as follows:

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


Accessing Elements

Array elements can be accessed using the index that starts with 0. Let suppose you want to access the third element of a single dimensional array. Then the index will be 2.

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


Example

The following example demonstrates the declaration, initialization, and accessing the elements of a single dimensional array:

public class Main {

    public static void main(String[] args) {

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

        System.out.println("Third element is: " + numbers[2]);

    }

}


When you run the above code, the output will be:

Third element is: 30


Two-Dimensional Array

A two-dimensional array stores data in the form of rows and columns. It is mainly used to represent matrix or tabular data. Each element in a two-dimensional array is accessed using two index values.


Declaration

The two dimensional array is declared as follows:

int[][] matrix;


Initialization

You can initialize the two dimensional array as follows:

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


Accessing Elements

To access elements in a two dimensional array, you need both row and column index. For example, to access the element 6:

System.out.println(matrix[1][2]);


Example

The following example demonstrates a two dimensional array:

public class Main {

    public static void main(String[] args) {

        int[][] matrix = {

            {1, 2, 3},

            {4, 5, 6}

        };

        System.out.println("Element is: " + matrix[1][2]);

    }

}


When you run the above code, the output will be:

Element is: 6


Multidimensional Array

A multidimensional array in Java is an array with more than two dimensions. It is used when data needs to be represented in three or more levels. Each additional dimension requires another index to access elements.


Declaration

The three dimensional array is declared as follows:

int[][][] data;


Initialization

You can initialize a three dimensional array as follows:

int[][][] data = new int[2][3][4];


Example

The following example demonstrates a three dimensional array:

public class Main {

    public static void main(String[] args) {

        int[][][] data = new int[2][2][2];

        data[0][1][1] = 100;

        System.out.println("Value is: " + data[0][1][1]);

    }

}

When you run the above code, the output will be:

Value is: 100


Jagged Array

A jagged array is a special type of two dimensional array where each row can have a different number of columns. In Java, a two dimensional array is actually an array of arrays, which makes jagged arrays possible.


Declaration

The jagged array is declared as follows:

int[][] jagged = new int[3][];


Initialization

You can initialize each row separately as follows:

int[][] jagged = new int[3][];
jagged[0] = new int[2];
jagged[1] = new int[4];
jagged[2] = new int[3];


Example

The following example demonstrates a jagged array:

public class Main {

    public static void main(String[] args) {

        int[][] jagged = new int[3][];

        jagged[0] = new int[]{1, 2};

        jagged[1] = new int[]{3, 4, 5, 6};

        jagged[2] = new int[]{7, 8, 9};


        System.out.println("Element is: " + jagged[1][3]);

    }

}


When you run the above code, the output will be:

Element is: 6


Difference Between Different Types of Arrays

The following table highlights the differences between Single-Dimensional, Two-Dimensional, Multidimensional, and Jagged arrays in Java. Each type varies in structure, indexing, memory representation, and real-world usage.

Basis of Comparison

Single-Dimensional Array

Two-Dimensional Array

Multidimensional Array

Jagged Array

Definition

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

A two-dimensional array stores elements in rows and columns using two indices.

A multidimensional array stores elements in three or more dimensions using multiple indices.

A jagged array is a two-dimensional array where each row can have a different number of columns.

Structure

The structure of a single-dimensional array is linear.

The structure of a two-dimensional array is tabular or matrix-like.

The structure of a multidimensional array extends beyond rows and columns into multiple levels.

The structure of a jagged array consists of arrays within an array having unequal lengths.

Indexing

A single index is required to access an element in a single-dimensional array.

Two indices are required to access an element in a two-dimensional array.

More than two indices are required to access an element in a multidimensional array.

Two indices are required to access elements in a jagged array.

Memory Allocation

Memory in a single-dimensional array is allocated in a continuous block.

Memory in a two-dimensional array is allocated as an array of arrays.

Memory in a multidimensional array is allocated as nested arrays.

Memory in a jagged array is allocated separately for each row.

Complexity

A single-dimensional array is simple to declare and use.

A two-dimensional array is moderately complex compared to a single-dimensional array.

A multidimensional array is more complex due to multiple levels of indexing.

A jagged array provides flexibility but requires careful initialization.

Example Declaration

The declaration of a single-dimensional array is int[] arr;.

The declaration of a two-dimensional array is int[][] arr;.

The declaration of a multidimensional array is int[][][] arr;.

The declaration of a jagged array is int[][] arr = new int[3][];.

Common Use

A single-dimensional array is commonly used to store lists of values.

A two-dimensional array is commonly used to represent matrices and tables.

A multidimensional array is commonly used in scientific and 3D computations.

A jagged array is commonly used when rows require different sizes.


Author Info 

Sudhir Sharma is a programming educator with strong expertise in technical content development and SEO. He has extensive experience in creating simplified tutorials on Java, C, C++, Python, Data Structures, DBMS, and other computer science subjects.

He focuses on breaking down complex programming concepts into easy-to-understand explanations with practical examples, helping students, beginners, and professionals strengthen their coding skills and prepare for interviews.


LinkedIN: https://www.linkedin.com/in/sudhir-sharma-b758aa81/


No comments:

Post a Comment