Java Array

Array is used when same type of multiple values has been stored in one variable.

Like, we want to render or store 10 int numbers (values in general). Thus we have to create 10 variables for each number.


Like:
int num1 = 100;
int num2 = 200;
int num3 = 300;
------
------
int num10 = 1000;

For 10 variables, it is okay may be. But what if, you want to store 100 or even 1000 such values and required particular variable for each value?

In this case, we can use array in java to store hundreds of values at a time in a single variable, instead of creating variable for storing each value.

Array declaration: How we decalare variable in java, similarly we can also decalare array in java. But, we use extra square bracket [].


data_type array_name []; // or
data_type [] array_name; 
//Like 
int [] numbers; 
String [] fruits; 

Array initialization:
We can initialize array at the time of declaration or using array indexes to initialize array elements.

Note: Each member of an array is called element of array in general. And, the positions they are stored are called cells. So, we can say array elements is stored in array cell. Or whatever!

Like:


//declaring an array 
int [] numbers; //without size defined
int [] numbers = new int [5]; //with size defined
//You can't add 6 number elements in this array now
//initialize or add data members in it
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

So, we have initialized or added data members to the numbers by using indexing.

Note that, array index starts with 0. It means, the first member of the array array[0], second member is array[1], third member is array[2], likewise the last member is array[length-1]

Note: Here, array means the name of the array. And length means the size of the array. The last member or element of the array is 1 less than the length of the array.

Like, in the above array initialization, we have added total 5 elements to our numbers array. So, the length of the numbers array is 5. But, the total indexes are 4. Because, array index or position starts from 0 to length -1.

Array initialization at the time of declaration:

How?

Instead of writing each data member individually by accessing the index number we can add the members at the time of declaration.

Like:


//declaring an array and initialize it
int [] numbers = {10, 20, 30, 40, 50}; 
//Or
int [] numbers = new int [] {10, 20, 30, 40, 50}; 

Here, you can't add the size (dimension expression) and initialization together.

Like:


int [] numbers = new int [5] {10, 20, 30, 40, 50}; //it's illegal.

But:


int [] numbers = new int [5]; //just array declaration would be fine.

Okay.

Accessing array elements:

Each member of the numbers array are called elements. And we use array index to access the elements stored in the array.

For accessing first member we can use the syntax: numbers[0] For accessing the second element we can use the syntax: numbers[1] and for accessing the last member or element of the numbers array, we can use either numbers[4] or numbers [numbers.length - 1];

Note: array index is always 1 less than the length of the array or alternative.

Example 1:

Creating an array that stores 10 int numbers;


public class Main{
  public static void main (String[] args){
    //creating an array and initialize it
    int [] numbers = {10, 20, 30, 40, 50};
    
    //print the array members or 
    //elements in the console
    
    //printing the first element
    System.out.println("First element is "+ numbers[0]);
    //printing the second element/number
    System.out.println("Second element is "+numbers[1]);
    
    //printing the last elements
    System.out.println("Last element is"+ numbers[4]);
    //print the last element using
    //arrays length method
    System.out.println("Last element is "+numbers [numbers.length - 1]);
    
  }
}
/**
 * Output: 
 * First element is 10
 * Second element is 20
 * Last element is 50
 * Last element is 50
 * 
 */

Example 2:

Array iteration using for loop and foreach loop:

Instead of printing array members to the console using index number, we can also use for loop or enhanced for loop to print all the elements in the console.

Let's create an array of String that stores names of fruits and iterate it through for loop.


public class Main(){
  public static void main (String[] args){
  //creating array of fruits
  String [] fruits = {"Apple", "Banana", "Orange", "Mango", "Grape", "Kiwi", "Cherry", "Pineapple", "Papaya", "Blueberry"}; 
  
  //loop through the array
  //using for loop
  System.out.println("Fruits List: (using for loop)");
  for(int i = 0; i< fruits.length; i++){
    System.out.println(fruits[i]);
  }
  
  //iterate the fruits array using
  //enhanced for/foreach loop
  System.out.println("Fruits List: (using foreach loop)");
  for(String fruit: fruits){
    System.out.println(fruit);
  }
  }
}
/**
 * Output:
 * Fruits List: (using for loop)
 * Apple
 * Banana
 * Orange
 * Mango
 * Grape
 * Kiwi
 * Cherry
 * Pineapple
 * Papaya
 * Blueberry
 * 
 * Fruits List: (using foreach loop)
 * Apple
 * Banana
 * OrangeMango
 * Grape
 * Kiwi
 * Cherry
 * Pineapple
 * Papaya
 * Blueberry
 * 
 */

# Changing the Array elements or reinitialize the array elements or members

How to do it?

First of all we have to know that we can't change the size of the array but we can change the array elements individually. For this, we can use array index with specified value.

Guess, we have a cars array and it has total 5 members.

String [] cars = {"Volvo", "Mustang", "Maruti", "BMW", "Honda"};

Now, changing the first element of this array we can use the syntax: cars[0] = "Audi";

Changing the second element we can use the syntax: cars [1] = "Ford";

Likewise the above syntax, we can change the members of the array referring the index number.

Example 3: (Reinitialize the array elements)


public class Main{
  public static void main (String[] args){
    
    //cars array 
    String [] cars = {"Volvo", "Mustang", "Maruti", "BMW", "Honda"};
    
    //print the first element
    System.out.println("First Car before changing is: "+cars[0]);
    
    //change the first element of the
    //cars array
    cars[0] = "Audi";
    //now print the first element
    System.out.println("First Car after changing is: "+cars[0]);
  }
}
/**
 * Output:
 * First car before changing is: Volvo
 * First car after changing is: Audi
 */

read more