Arrays in Java

In this tutorial, we will learn about Arrays in Java. Till now we learned about variables. We know in a variable we can store a value, for example, int num=10; Here num is variable of int type in which we can store an integer value. but suppose you want to store 100 values. It means you should have 100 variable. It will so though to create and manage 100 variables. So to overcome this kind of problem Java has a concept called Arrays.
In an Array, we can store multiple values using a single identifier. In an Array, values get store according to the index numbers. Let’s check syntax to define and initialize an array in Java.

Syntax to define Array in Java:

int marks[5]={20,25,24,30,15};

Index system of Array:

We discussed earlier how in array values stored on the index. In the following figure, you can see how values get stored on different index numbers. Index number always starts from 0.

Access Array Element:

To access an array element, we have to mention the index number. Then we can fetch value of that particular in the index in the array. Check this Example.

Example Program:

class ArrayExample{
  public static void main(String[] args) {
    int[] marks[5]= {10,15,12,19,20};
    System.out.println(marks[2]);
    //output would be 12 because 12 on index 2 (index starts from 0)
  }
}

Array with loop:

Here we will check how we can use a loop to access all the elements of the array. Check this example.

Example Program:

class ArrayExample{
  public static void main(String[] args) {
    int[] marks[5]= {10,15,12,19,20};
       for(int i=0;i<5;i++){
          System.out.println(marks[i]);
       }
   }
}

Multi-dimensional Array:

In very simple language multi-dimensional arrays are known as an array of arrays. In a multi-dimensional array, we store data in the form of rows and columns. Check out the syntax to define multi-dimensional arrays in Java.

Syntax to define Array in Java:

data_type name_of_array[1st dimension][2nd dimension][Nth dimension]=new data_type[size of first dimension][size of second dimension][size of Nth dimension];

Example of Multi-dimensional Array in Java:

//defining two dimensional array
int arr[][]=new int[2][2];

//defining and intlizing two dimensional array
int arr[][]={  {10,15} , {13,18}  };

//defining three dimentional array
int[][][] arr =new int[2][2][2];

//defining and intlizing three dimensional array
int[][][] arr = {   {  {2, 4}, {6, 5}  }, {  {3, 4}, {6, 9}  }

Example Program to add elements in a two-dimensional array:

import java.util.Scanner;
class Example { 
    public static void main(String[] args) 
    { 
        Scanner scanner=new Scanner(System.in);
        int[][] arr = new int[2][2]; 
        for (int i = 0; i < 2; i++){
            for (int j = 0; j < 2; j++) {
              arr[i][j]=scanner.nextInt();
            }
          }
                
    } 
}

Example Program to fetch elements of a two-dimensional array:

class Example { 
    public static void main(String[] args) 
    { 
        int[][] arr = { {50,10},{20,35}}; 
        for (int i = 0; i < 2; i++){
            for (int j = 0; j < 2; j++) {
              System.out.println(arr[i][j]);
            }
          }
                
    } 
} 

 

Spread the love
Scroll to Top