Array in Java is the collection of similar type data values. In java, array treat as an object. To declare an array, define the variable type with square brackets. Variables in the array are order, and each has an index beginning from 0. The size of an array must be specified by int or short value and not long value.

Declaration of Array

There are two types of declaration of array.

  • Static declaration
  • Dynamic declaration

Static Declaration Array

Size will be fixed during declaration. An array that is declares with the static keyword. Static arrays are allocated memory at compile time.

Syntax

<data type> <variable name> []={<data1>,<data2>,.....<dataN>};  
Syntax Static Declaration Array

Example Static Declaration Array

Input Value

public class arrays {
    private static String[] array;  
static   
{  
array = new String[2];  
array[0] = "Hello Student";  
array[1] = "Basicengineer";  
}  
public static void main(String args[])   
{  
for(int a = 0; a < array.length; a++)  
{  
System.out.print(array[a] + " ");  
}  
} 
}
Static Declaration Array

Let’s run the code

Input Values

Output Value

Output Values

Dynamic Declaration Array in Java

Array can be declare without size(empty) and size can be determine later declaration. It is locates in heap memory space.

Syntax

<data type> <variable name> [ <No. of elements to be stored in array>];
Syntax of Dynamic Declaration Array

Example Dynamic Declaration Array

Input Value

public class arrays {
    public static void main(String[] args)   
    {  
    int array[];  
    array= new int[8];  
    array[0] = 5;  
    array[1] = 10;  
    array[2] = 15;  
    array[3] = 20;  
    array[4] = 25;  
    array[5] = 30;  
    array[6] = 35;
    array[7] = 40;
    System.out.print("Elements of Array: ");  
    for(int i=0; i< array.length ; i++)   
    {  
    System.out.print(array[i] +" ");  
    }  
    }  
}
Dynamic Declaration Array

Let’s run the code

Input Values

Output Value

Output Values

Types of Array in Java

There are two types of Array.

  • Single Dimensional Array
  • Multidimensional Array

Single Dimensional Array

A single dimensional array is a collection of similar types of element store at contiguous memory locations.

Syntax

dataType[] arr; 
(or)  
dataType []arr;
Syntax of Single Dimensional Array

Example of Single Dimensional Array

Input Value

public class arrays {
    public static void main(String args[]){  
        int a[]=new int[7]; 
        a[0]=5;  
        a[1]=10;  
        a[2]=15;  
        a[3]=20;  
        a[4]=25;  
        a[5]=30;
        a[6]=35;
        for(int i=0;i<a.length;i++)
        System.out.println(a[i]);  
        }
}
Single Dimensional Array

Let’s run the code

Input Values

Output Value

Output Value

Multidimensional Array

The Data in multidimensional arrays are stored in tabular form.

Syntax

data_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new data_type[size1][size2]….[sizeN];
Syntax of Multidimensional Array

Example

Input Value

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

		int[][] arr = { { 11, 12 }, { 13, 14 } };

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

			System.out.println();
		}
	}


}
Multidimensional Array

Let’s run the code

Input Value

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *