Introduction
- An array is a linear data structure.
-
In a linear data structure, data is stored in a specific manner.
- It is a finite and homogeneous collection of elements.
-
We cannot change the size of an array once it is declared, making it finite.
- It is homogeneous because it stores the same type of elements.
- In Java, programmers use arrays to store multiple elements into a single variable.
How does an Array in Java work?
We understand that arrays store homogeneous information in a single variable. Let’s illustrate this with an example.
java//storing data using variable int data1=23; int data2=15; int data3=46; int data4=7;
In Java, all information is stored using variables. In the example above, we utilized four different variables to store the same type of information. Alternatively, we can store this information using an array, as demonstrated below.
java//storing data using array int[] data=new int[4]; data[0]=23; data[1]=15; data[2]=46; data[3]=7;
To frequently access this data, we must assign the array a specific identifier. Thus, array enables us to store multiple values into a single variable.
Ways to declare Array in Java
We can create array in two ways in java
- Using new keyword
- Array literal
Using new keyword
We have to follow these steps for the creation of an array.
- Declaration:
- First we have to create an array variable using appropriate primitive or non-primitive data type
- example: int[] data;
- Size Allocation:
- After declaration it is mandatory to allocate size for array variable
- Size must be an integer value
- example: data= new int[5];
- Initialization:
- In order to initialize array variable we have to provide a specific index number.
- example: data[0]=23; data[1]=15;
Example:
javapublic class ArrayDemo1 { public static void main(String[] args) { //declaration int[] data; //size allocation data =new int[5]; //initialisation data[0]=10; data[1]=20; data[2]=30; data[3]=40; data[4]=50; //print information System.out.println(data[0]); System.out.println(data[1]); System.out.println(data[2]); System.out.println(data[3]); System.out.println(data[4]); System.out.println("============================="); } }
Array Literal
- It is the different approach to create an array.
- In the case of array literals, we can add elements into an array without providing size or capacity
Example:
javaimport java.util.Scanner; public class ArrayDemo2 { public static void main(String[] args) { Scanner sc1=new Scanner(System.in); int[] data={5,25,35,45}; System.out.println(data[0]); System.out.println(data[2]); } }
Iterating through an array in Java
We can access elements of an array using different methods
javapublic class ArrayDemo3 { public static void main(String[] args) { int[] arr={10,20,30,40,50}; //regular for loop for(int i=0; i<=arr.length; i++){ System.out.println(arr[i]) } //Enhanced for loop for(int a:arr){ System.out.println(a); } } }
- Regular for loop: To iterate through an array using a for loop in Java, you need to follow this general syntax:
int i = 0;initializes the loop variable.i <= arr.length-1;defines the condition for the loop to continue iterating until the index i reaches the length of the array.- Here, arr.length calculates the length of an array. And since the array index starts from zero, we use
arr.length - 1to access the last element in the array. i++increments the loop variable after each iteration.- Inside the loop, you can access each element of the array using the index
i
- Enhanced for loop:
You can iterate through an array using an enhanced for loop (also known as a for-each loop) in Java.
The enhanced for loop in Java is designed specifically for iterating over arrays and collections.
It provides a simpler and cleaner syntax compared to traditional for loops when you need to iterate over all elements in an array.
In this loop:
int adeclares a variable a of the same type as the elements in the array.: arrspecifies the array you want to iterate over.Inside the loop, you can directly access each element of the array using the variable a. The loop will iterate through each element of the array sequentially.
Types of Array in Java
Array can be classified based on their dimensionality and data types as mentioned below.
Classification based on data type:
Primitive Type Array: An array that stores elements of primitive data types such as int, char, float, double, etc.
Object Type Array: An array that stores elements of object types, such as instances of classes, strings, or other arrays.
Classification based on dimensionality:
Single-Dimensional Array: This type of an array stores. It is the most basic form of an array in Java.
Example:
javaimport java.util.Scanner; //EXAMPLE FOR SINGLE DIMENSIONAL ARRAY public class ArrayDemo3 { public static void main(String[] args) { Scanner sc1= new Scanner(System.in); System.out.println("Enter number of elements"); int sizeOfArray=sc1.nextInt(); //Data is stored in rows int[] data=new int[sizeOfArray]; //Accept each element from user for (int a=0; a<count; a++){ System.out.println("Enter Value"); int value=sc1.nextInt(); data[a]=value; } System.out.println("=================="); System.out.println("Printing values"); for (int b=0; b<count; b++){ System.out.println(data[b]); } } }
- Multi-Dimensional Array: An array that contains other arrays as its elements, forming a matrix-like structure. Common examples include 2D arrays, 3D arrays, and so on.
- 2-D Array:
- 2D array is a type of multidimensional array.
- 2D array is mainly used to store the data in terms of rows and columns.
- Following is the process to create 2D array
- 2-D Array:
- Declaration:
- We can declare an array variable by providing appropriate data type.
- example: int[][] arr;
- Size Allocation:
- After declaration we have to allocate size for 2D array.
- At the time of size allocation we have to provide row and column count.
- example: arr= new int[noOfRow][noOfCol];
- Initialization:
- To initialize we have to provide index number of row and column.
- example: arr[0][0]=10; arr[0][1]=20;
- Declaration:
Example:
javapublic class ArrayDemo1 { public static void main(String[] args) { //Step 1:Declaration String[][] arr; //Step 2: Size Allocation (rows=5 and column=2) arr=new String[4][2]; //Step 3:Initialization arr[0][0]="MAHARASHTRA"; arr[0][1]="MUMBAI"; arr[1][0]="KARNATAKA"; arr[1][1]="BANGALORE"; arr[2][0]="PUNJAB"; arr[2][1]="CHANDIGARH"; arr[3][0]="GOA"; arr[3][1]="PANJI"; //Printing the values System.out.println("STATE\t\tCAPITAL"); for(int a=0; a< arr.length; a++){ for (int b=0; b< 2; b++){ System.out.print(arr[a][b]+"\t"); } System.out.println(); } } }
- 3-D Array:
- It is also a part of multidimensional array.
- In case of 3D array data will be store in the form of rows, columns and panels
- Following are the steps to create a 3D array
- Declaration:
- int[][][] data;
- Size Allocation:
- data=new int[rows][cols][panels];
- Initialization:
- data[0][0][0]=10;
- data[0][0][1]=20;
- Declaration:
- 3-D Array:
javaimport java.util.Scanner; public class Array3D { public static void main(String[] args) { Scanner sc1= new Scanner(System.in); //No of rows System.out.println("Enter Number of Pages"); int page=sc1.nextInt(); //no of cols System.out.println("Enter Number of lines on each page"); int line=sc1.nextInt(); //no of panels System.out.println("Enter Number of words on each line"); int word=sc1.nextInt(); String[][][] notebook=new String[page][line][word]; //initializing (Accepting the value from the end user) for(int a=0; a< notebook.length; a++){ for(int b=0; b<notebook[a].length; b++){ for(int c=0; c<notebook[a][b].length; c++){ notebook[a][b][c]= sc1.next(); } } } //Printing the 3d array for(int a=0; a< notebook.length; a++){ System.out.println("PAGE NO "+(a+1)); for(int b=0; b<notebook[a].length; b++){ for(int c=0; c<notebook[a][b].length; c++){ System.out.print(notebook[a][b][c]+"\t\t"); } System.out.println(); } System.out.println(); } } }
What is ArrayIndexOutOfBoundException?
- It is a class declared inside java.lang package
- It is a type of runtime exception thrown by JVM.
- If a programmer is trying to assign a value at invalid position(index) then JVM will throw this exception.

