Imagine if you want to declare a large number of variables of the same type ( Let us say N ), it is tedious to declare variables like var1, var2, var3 ......varN. Arrays will help you in avoiding the tedious effort of declaring N variables.
Array is a collection of similar variables. Size of the array can be defined, declaring an array of size N is equal to declaring N variables. Each array element is identified using an index. For an array with N values, the first element has an index of 0, the second element has the next index value of 1, and in the end, the last element has an index of (N-1). We can easily navigate, and update the integer values stored in this array.
Let us look at the below code snippet and understand the concept of arrays.
Syntax for declaring an array . <array_type> <array_variable_name>[] = new (special operator) <Array type could be any basic data type, example int, double, float etc. int arrayVar[] = new int[5]; double arrayVar[] = new double[5]; int arrayVar[]; // Declaring an array. arrayVar = new int[5]; // Allocating memory for (5 entries) an Array. /* The below statement can replace the above 2 statements */ /* Single statement can be used to declare and allocate the space for an array*/ int arrayVar[] = new int[5]; // This is how you declare and allocate space for an array. arrayVar[0] = 237; arrayVar[2] = 101; arrayVar[3] = 880; arrayVar[1] = 680; arrayVar[4] = 85;
Couple of things to keep in mind, adding elements to an array need not be done sequentially, you may add elements in any order. For example 0th index of an array could be filled at the end. Also, memory for the array is allocated dynamically using the "new" operator. We will see more about the new operator in the coming sections.
The arrays we have seen so far are single dimensional arrays. An array can also be a multi dimensional array. A multidimensional array is simply an array of arrays. Let us see how a multi dimensional array looks like.
Let us look at the below code snippet and understand the concept of multi dimensional arrays.
Syntax for declaring an array . <array_type> <array_variable_name>[][] = new (special operator) <Array type could be any basic data type, example int, double, float etc. int arrayVar[][] = new int[5][5]; double arrayVar[][] = new double[3][5]; Let us the index of an array and store the values. int arrayVar[][] = new int[2][3]; arrayVar[0][0] = 880; arrayVar[0][1] = 680; arrayVar[0][2] = 280; arrayVar[1][3] = 101;
You may not use the entire array. Few spaces in the array may not be filled with any data and could be empty.
A multi dimensional array need not be as show in the below figure, we may want to have more flexibility with a multi dimensional array. Java's flexibility happily meets our demand, we can also have a multi-dimensional array, where each array is of different depth. Let us see another 2-dimensional array but this time with variable length.
Here is an other way of declaring an array . <array_type> <array_variable_name>[][] = new (special operator) <Notice, mentioning the size of 2nd dimension is optional. Array type could be any basic data type, example int, double, float etc. int arrayVar[][] = new int[5][]; double arrayVar[][] = new double[3][]; This how a variable 2 dimensional array (like shown in the above figure) is declared. arrayVar[0] = new int[10] // Size of the 2nd dimension declared here is 10. arrayVar[1] = new int[4] // Size of the 2nd dimension declared here is 4. arrayVar[2] = new int[6]
Let us write a sample program, using the concepts we have seen so far. We will also use the concepts like array.length etc.
public class ArrayExample { public static void main(String[] args) { int arrayVar[] = new int[4]; arrayVar[0] = 880; arrayVar[3] = 101; /* * It is not mandatory to fill the entire array with data. * Few spaces could be left un-used, unused spaces are usually * initialized to 0, but you cannot bank on it. */ System.out.println("One Dimensional Array:"); System.out.println("First Element:" + arrayVar[0] + "; Fourth Element:" + arrayVar[3] ); // arrayVar.length - Returns the length of the array. System.out.println("Length of the array " + arrayVar.length); // Another way of declaring an array. // int arrayVar2[][] = new int[3][4]; int[][] arrayVar2 = new int[5][4]; arrayVar2[0][3] = 237; arrayVar2[3][1] = 880; System.out.println("\nTwo Dimensional Array:"); System.out.println("First Row, 4th Column Element:" + arrayVar2[0][3] + "; Fourth Row, 2nd Column Element:" + arrayVar2[3][1]); int[][] arrayVar3 = new int[6][]; arrayVar3[5] = new int[10]; arrayVar3[0] = new int[4]; arrayVar3[0][3] = 680; arrayVar3[5][9] = 85; System.out.println("\nVariable Multi Dimensional Array:"); System.out.println("First Row, 4th Column Element:" + arrayVar3[0][3] + "; Sixth Row, 10th Column Element:" + arrayVar3[5][9]); } }
Compile/Run this program and here is the output:
One Dimensional Array: First Element:880; Fourth Element:101 Length of the array 4 Two Dimensional Array: First Row, 4th Column Element:237; Fourth Row, 2nd Column Element:880 Variable Multi Dimensional Array: First Row, 4th Column Element:680; Sixth Row, 10th Column Element:85