Array - In computer science, an array is a collection of objects with same data type or we can say array as a container that can hold a fixed number of objects. Example: Below we can see an array containing 7 integer values and index is starting from 0.
Image a: Array Representation
Properties of array:
- Array is simplest, most used data structure and used for implementing other data structures as list, stacks, queues etc.
- Data type must be same.
- Length is fixed.
- Length must be specified during array initialization.
- Each object stored at an index.
- Array index starts from zero.
- Object can be retrieved directly by index number.
Types of array:
- One-dimensional array - 1-D or single dimensional array is a linear array, they can represent any row or column.
Example : int arr = new int[5]; - Multi-dimensional array - Multi-Dimensional array is an array of array like 2-D array, 3-D array. Each element of multi-dimensional array is an array. Below we have an example of 2-D array in JAVA which can hold 9 elements, a 2D array holds rows and columns both and index always starts from 0. lets see:
Example: int[][] a = new int[3][3];
Image b: 2-D Array
- Java Code for 2-D array:
public class TwoDimensionalArray {
public static void main(String[] args) {
/*
Declaration and initialization of array
int [][] array;
int[][] a = new int[3][3];
int[][] b = new int[][]{
{3, 3, 5},
{2, 6, 1},
{5, 7, 8},
};*/
int[][] arr = {
{3, 3, 5},
{2, 6, 1},
{5, 7, 8},
};
/*
* printing multi dimensional array with old technique
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.print(" \n");
}
*/
/* Printing multi dimensional array with for...each loop */
for (int[] innerArray : arr) {
for (int data : innerArray) {
System.out.print(data + " ");
}
System.out.print(" \n");
}
}
}
Output:
3 3 5
2 6 1
5 7 8
Java Code for 3-D array:
public class ThreeDimensionalArray {
public static void main(String[] args) {
/*
Declaration and initialization of array
int [][][] array;
int[][][] a = new int[3][3][3]; // total 27 elements can be stored
int[][][] b = new int[][][]{
{
{3, 3, 5},
{2, 6, 1}
},
{
{-3, -3, -5},
{-2, -6, -1}
}
};*/
int[][][] arr = {
{
{3, 3, 5},
{2, 6, 1},
{8, 4, 7}
},
{
{-3, -3, -5},
{-2, -6, -1},
{-8, -4, -7}
},
{
{3, 3, 5},
{2, 6, 1},
{8, 4, 7}
}
};
/** printing multi dimensional array with old technique
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
for (int k = 0; k < arr[i].length; k++) {
System.out.print(arr[i][j][k] + " ");
}
System.out.print(" \n");
}
System.out.print(" \n");
}*/
/* Printing multi dimensional array with for-each loop */
for (int[][] outerArray : arr) {
for (int[] innerArray : outerArray) {
for (int data : innerArray) {
System.out.print(data + " ");
}
System.out.print(" \n");
}
System.out.print(" \n");
}
}
}
Output:
3 3 5
2 6 1
8 4 7
-3 -3 -5
-2 -6 -1
-8 -4 -7
3 3 5
2 6 1
8 4 7
Most Important Interview Questions of Array-
Comments
Post a Comment