Prev Next
- Array is an object, which stores a group of elements of the same data type.
- Array is index based and the first element of an array starts with the index 0.
- The size of array is fixed.
- Array is Index based and hence accessing a random element and performing any operations over the elements such as sorting, filling etc, can be easily performed.
- The size of an array is fixed and hence cannot grow or shrink at runtime.
Types of Arrays:
Arrays are generally categorized into two forms. They are,
- Single Dimensional Arrays
- Multidimensional Arrays
Single Dimensional Arrays:
Array creation, instantiation and initialization:
Syntax for declaration:
DataType[] Array_name; |
Example:
int[ ] Roll;
Char[ ] Name;
Syntax for instantiation:
Array_name = new datatype[size]; |
Example:
Roll = new int[10]; //can store 10 integer values in the array ‘Roll’
Declaration and instantiation can be done in a single line.
Example: int Roll[ ] = new int[5];
Array initialization:
Roll[1 ] = 101;
Roll[2] = 102;
Array declaration, instantiation and initialization can be done in a single line.
Example:
int arr[ ] = {45,56,67,65,44}; // arr[] has 5 elements
Accessing Elements of an array:
Array elements can be accessed by using the index value of the element.
Example:
Roll[4] denotes the element in the 5th position since arrays starts with the index 0. Generally, ‘for’ loop or ‘foreach’ loop is used to iterate over the elements of an array.
Example of Single Dimensional Array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class ArrayExample{ public static void main(String args[]){ int arr[] = new int[5]; //declaration, instantiation //initialization of array using for loop for(int i = 0; i<arr1.length; i++) { //length is a property of array (returns size) arr[i] = i; } for(int i=0;i<arr.length;i++) //printing array System.out.println(arr[i]); } } |
Output:
0 1 2 3 4 |
Multidimensional Arrays (2D,3D,…Arrays):
Arrays can have more than one dimension, which means arrays can themselves contain arrays i.e. Array of arrays.
Declaration, instantiation and initialization:
Example:
int[][] a = new int[2][2]; //2 Dimensional array with 2 rows & 2 column
a = {{10,20},{30,40}}; //initialization
which means,
a[0][0] = 10, a[0][1] = 20, a[1][0] = 20 & a[1][1] = 40
Two dimensional arrays are treated like matrix. The first index represents the row and the second represents the column.
Similarly, 3D array can be declared and initialized as,
int[ ][ ][ ] arr = new int [2][2][3];
arr = {{{45,95,78}, {53,56,57}}, {{98,90,95}, {72,60,85}}};
This can be imagined as marks of 3 subjects of first 2 students in 2 classes.
Class 1:
Student1: 45,95,78
Student2: 53,56,57
Class2:
Student1: 98,90,95
Student2: 72,60,85
Example of Multidimensional Array:
1 2 3 4 5 6 7 8 9 10 11 12 |
class MultiDimArray{ public static void main(String args[]){ int marks[][]={{98,90,95},{53,56,57}}; //declaring and initializing 2D array for(int i=0;i<2;i++){ for(int j=0;j<3;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } } |
Output:
98 90 95
53 56 57 |