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
Syntax Static Declaration Array<data type> <variable name> []={<data1>,<data2>,.....<dataN>};
Example Static Declaration Array
Input Value
Static Declaration Arraypublic 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] + " "); } } }
Let’s run the code
Output Value
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
Syntax of Dynamic Declaration Array<data type> <variable name> [ <No. of elements to be stored in array>];
Example Dynamic Declaration Array
Input Value
Dynamic Declaration Arraypublic 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] +" "); } } }
Let’s run the code
Output Value
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
Syntax of Single Dimensional ArraydataType[] arr; (or) dataType []arr;
Example of Single Dimensional Array
Input Value
Single Dimensional Arraypublic 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]); } }
Let’s run the code
Output Value
Multidimensional Array
The Data in multidimensional arrays are stored in tabular form.
Syntax
Syntax of Multidimensional Arraydata_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new data_type[size1][size2]….[sizeN];
Example
Input Value
Multidimensional Arraypublic 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(); } } }
Let’s run the code
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
0 Comments