10X Sale
kh logo
All Courses
  1. Tutorials
  2. Java

Data Structure:Array

Updated on Sep 2, 2025
 
32,960 Views

Data structure: Array

A data structure is a specific way in which information can be efficiently organized in a computer.

For example, using the array data structure, we can store a list of items with the same data type.

An array is normally a set of comparable types of elements with a contiguous place of memory.

Java array is an object with comparable information type elements. It is a structure of information where comparable elements are stored. Only a fixed set of elements can be stored in a Java array.

Java array is index-based, the array's first elements are placed at index 0.

Advantages of Array in Java

  1. Code Optimization: It optimizes the code; we can effectively retrieve or sort the information.
  2. Random access: We can get any information in an index place.

Disadvantages of an Array in Java

  1. Size Limit: Only the fixed size of elements can be stored in the array. At runtime, it does not increase its size. In Java, which develops automatically, the collection framework is used to fix this issue.

Below is the way we can declare an array in Java:

Type[] name;

  1. name is an array name or identifier.
  2. type can be any data type like byte, short, char, int, etc. or an object.

Let's see the above example one more time.

int[] salary; 

Here, salary is an array that can hold values of type int.

How many elements can this array hold?

We haven't defined it yet. The next step is to allocate memory for array elements.

data = new int[5]; 

The length of the salary array is 5. Meaning, it can hold 5 elements (5 int values in this case).

Remember, once the array length is defined, the program cannot change it.

Java Array Index

Using indices, you can access components of an array. Let's take an instance below

short[] age = new short[5]; 

Java Array Index 

The first element of an array is age[0], the second is age[1] and so on.

If the length of an array is n, the last element will be arrayName[n-1]. Since the length of the age array is 5, the last element of the array is age[4] in the above example.

For numeric kinds, the default original value of array components is 0 and for boolean false.

In Java, during declaration, you can initialize arrays or you can initialize (or alter values) later in the program according to your need.

Initialize an Array During Declaration

Below is an example of how we can initialize an array during declaration in java program.

int[] age = {12, 4, 5, 2, 5}; 

This statement creates an array and initializes it during the declaration.

The length of the array is determined by the number of values provided which is separated by commas. In our example, the length of the age array is 5.

Initialize an Array During Declaration

Let's write an easy array element printing program.

class MyArrayProgram { 
public static void main(String[] args) { 
int[] age = {123, 441, 50, 21, 50}; 
for (int i = 0; i < 5; ++i) { 
System.out.println("Value at index " + i +": " + age[i]); 
      } 
   } 
}

When you run the program, the output will be:

Value at index 0: 123 
Value at index 1: 441 
Value at index 2: 50 
Value at index 3: 21 
Value at index 4: 50

By using its numeric index, we can readily access and modify array components.

There are two types of an array in java.

  1. Single Dimensional Array
  2. Multidimensional Array

Single Dimensional Array in Java

Syntax to Declare an Array in Java

  1. dataType []arr; (or)
  2. dataType[] arr; (or)
  3. dataType arr[];

Instantiation of an Array in Java

  • arrayRefVar=new datatype[size];

Multidimensional Array in Java

In such a case, data is stored in a row and column-based index (also known as matrix form).

Syntax to Declare Multidimensional Array in Java

  1. dataType arrayRefVar[][];
  2. dataType []arrayRefVar[];
  3. dataType[][] arrayRefVar;
  4. dataType [][]arrayRefVar;

Example to instantiate Multidimensional Array in Java

  • int[][] arr=new int[3][3]; //3 row and 3 column

ArrayIndexOutOfBoundsException in java

The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if the array length is negative, equal to or higher than the array size while passing through the array.

// Java Program to demonstrate the case of  ArrayIndexOutOfBoundsException in a Java Array.   
public class CheckArrayException{   
public static void main(String args[]){   
int arr[]={550,6,770,8};   
for(int i=0;i<=arr.length;i++){   
System.out.println(arr[i]);   
}   
}}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 
at CheckArrayException.main(TestArrayException.java:5) 
550 
6 
770 
8
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses