Array Initializer provides a way to initialize the array elements with values, the moment it is declared. This removes the two step process of declaring and then initializing the values. It also automatically takes care of the length of the array.
With out using the array initializer, we have to do the following if we want to create a marks array for a class with 4 students.
int marks[] = new int[4]; int marks[] = {87, 76, 84, 57}; int marks[] = new int[4]; // declared separately If we want to assign to an existing array we need to do the following. int marks[] = null; // declared separately Array initializer can also be used to initialize multi dimensional arrays. Below is an example marks obtained by 3 students in 4 subjects. int marks[][] = {
|