Skip to content

Arrays In Java Programming Language

In this article, you will learn arrays in java programming language which include, types of arrays, how to declare, create and execute arrays, and the static methods used in arrays class.

So, let’s first understand what is an array in java programming language before moving to the above topics.

Also Read:- How to Get Current Date and Time in Java ?

“An array is defined as a collection of similar type of elements(variables) having a fixed length”. It reduces the code by declaring variables of similar data type in a single unit rather than declaring them individually.

Each element in the array is stored according to its index value which begins with ‘0’. The first array element is stored at index ‘0’, second array element is stored at index ‘1’ and so on.

These index values help to access the array elements randomly. To make it more clear to you, we have shown below a pictorial representation of array elements with their corresponding index values.

arrays-in-java

TYPES OF ARRAYS IN JAVA

Basically, there are two types of arrays in java programming language.

  1. Single-Dimensional Array
  2. Multi-Dimensional Array

[1] SINGLE-DIMENSIONAL ARRAY

The following three steps are used to create single-dimensional array.

Step 1: Declaring a Single-dimensional array:

The following syntax is used to declare a single-dimensional array in java.

dataType[] arrVariable ;

Step 2: Instantiating Single-dimensional array:

You can instantiate or create a single dimensional array by using the ‘new’ keyword whose syntax is given below.

arrVariable = new dataType[arrSize]

Step 3: Initializing Single-dimensional array:

arrVariable[0] = value1 // initializing the first element
arrVariable[1] = value2 // initializing the second element, and so on.

You can also use the following syntax to declare, instantiate and initialize an array.

dataType[] arrVariable = {value1, value2, value3…..}

Example 1:

class ArrayExample {
public static void main(String[] args) {

int[] arrVariable; //declaring an array

arrVariable = new int[5] ; //instantiating the array

arrVariable[0] = 15 ; //initializing the first array element
arrVariable[1] = 25 ; //initializing the second array element, and so on…
arrVariable[2] = 35 ;
arrVariable[3] = 45 ;
arrVariable[4] = 55 ;

System.out.println(“First element : “ + arrVariable[0]) ;
System.out.println(“Second element : “ + arrVariable[1]) ;
System.out.println(“Third element : “ + arrVariable[2]) ;
System.out.println(“Fourth element : “ + arrVariable[3]) ;
System.out.println(“Fifth element : “ + arrVariable[4]) ;
}
}

Output 1:

First element : 15
Second element : 25
Third element : 35
Fourth element : 45
Fifth element : 55

Example 2:

class ArrayExample {
public static void main(String args[]) {

int arr[]={15, 25, 35, 45, 55} ; //declaring, instantiating and initializing the array element

for(int i=0; i<5; i++)
System.out.println(arr[i]) ;
}
}

Output 2:

15
25
35
45
55

[2] MULTI-DIMENSIONAL ARRAY

The following three steps are used to create multi-dimensional array which consists rows and columns.

Step 1: Declaring a Multi-dimensional array:

The following syntax is used to declare a multi-dimensional array.

dataType[] [] arrVariable ;

Step 2: Instantiating Multi-dimensional array:

dataType[] [] arrVariable = new dataType[rowSize][columnSize] ;

Step 3: Initializing Multi-dimensional array:

arrVariable[0] [0] = 20 ;
arrVariable[0] [1] = 30 ;
arrVariable[1] [0] = 30 ;
arrVariable[1] [1] = 30 ;

Example:

class MultidimensionalArray {
public static void main(String args[]){

int arrVariable[][]={{15,25},{35,45},{55,65}};

for(int i=0; i<3; i++) {
for(int j=0; j<2; j++) {
System.out.print(arrVariable[i][j]+” “);
}
System.out.println();
}
}
}

Output:

15 25
35 45
55 65

STATIC METHODS USED IN ARRAYS CLASS

Java provides static methods for arrays class which comes along with the java.util.Arrays package.

Some of the static methods used in arrays class are listed below.

  • static int binary Search(Object[] a, Object key)
  • static boolean equals(long[] a, long b)
  • static void fill(int[] a, int val)
  • static void sort(Object[] a)
  • static boolean[] copyOf(boolean[] original, int newLength)
  • static int hashCode(byte[] a)

Also Read: Nested Classes in Java Programming Language

We have provided you the best possible information on arrays in java programming language. Hope you like post. For more updates and related information, stay connected to our blogs on java programming language.

Facebook
Twitter
LinkedIn
Pinterest