Arrays in Java

Table Of Contents :

Table of contents

No heading

No headings in the article.

  1. What is an array and its dimensionality types?

  2. Array declaration and syntax in Java

  3. Traversing an array

  4. What is an array and its dimensionality types?

    A Java array is a container object in Java. It is used to store objects of a single type as part of a single set. The data type of all array items in Java is the say, whether textual, integer, or decimal. To build a Java array, we must first determine the array's length. The length of an array in Java cannot be extended once it has been formed.

    There are two types of arrays, single-dimensional arrays have only one dimension, while multi-dimensional have 2D, 3D, and so on.

    Arrays are generally used when we have to deal with several variables that cant be manually declared one by one . So instead in case we know that the dataType of all the elements that we need to put are of the same datatype , we can instead make those many memory locations and access each one of those variables using the relative memory block distance it is from the starting element of the array container

  5. Array Declaration and syntax in Java

    In the declaration of an array in java , the two main components are

    ( i ) Declaring the dataType of the variables of the array

    ( ii ) Naming the reference of the array using same variable naming rules of Java

    Example : In case we want to have a data structure storing the outcome of the toss of a coin 5 times , then we can declare it in the following way :

// First Way ( Recommended )
String[] tossOutcome = new String[ 5 ] ;   

// Second Way 
Stting tossOutcome[] = new String [ 5 ] ;

In the above code snippet, the String[] part signifies that the datatype of the variables stored in the array are to be of String[] type and the square bracket signifies that the identifier is gonna refer to an array .

  1. Traversing the array

    Well, Just declaration of the array is not sufficient. Let us say that the outcome of the 5 tosses were "Head" , "Tail", "Tail" , "Head", "Tail" respectively

    The below code assigns the toss outcome results of the 5 tosses and displays them.

    
     import java.util.*; 
     public class Toss 
     {
         public static void main ( String [] args ) 
         {
             String[] tossOutcome = new String [ 5 ] ;
             tossOutcome [ 0 ] = "Head";
             tossOutcome [ 1 ] = "Tail"; 
             tossOutcome [ 2 ] = "Tail"; 
             tossOutcome [ 3 ] = "Head"; 
             tossOutcome [ 4 ] = "Tail";  
    
             for ( int i = 0 ; i < tossOutcome.length ; ++i )
             {
                 System.out.println ("\nToss number : " +(i+1)  ) ;
                 System.out.println ("Coin toss outcome : " +tossOutcome[i] );
                 System.out.println ("*******************") ;
             }
         }
     }
    

Updating values of the array :

Let us say that in the first toss, the outcome of the coin toss was not "Head" but was "Tail" instead. In that case it is very simple to change/modify/update the value.

The below code changes the outcome of the first toss from "Head" to "Tail" and displays the array before and after the change :

import java.util.*; 
public class Toss 
{
    public static void main ( String [] args ) 
    {
        // Array declaration
        String[] tossOutcome = new String [ 5 ] ;

        // Initialization of array values 
        tossOutcome [ 0 ] = "Head";
        tossOutcome [ 1 ] = "Tail"; 
        tossOutcome [ 2 ] = "Tail"; 
        tossOutcome [ 3 ] = "Head"; 
        tossOutcome [ 4 ] = "Tail";  

        // Printing the conToss ourcomes before modifying 
        System.out.println ("Initially the array elements were as follows" );
        for ( int i = 0 ; i < tossOutcome.length ; ++i )
        {
            System.out.println ("\nToss number : " +(i+1)  ) ;
            System.out.println ("Coin toss outcome : " +tossOutcome[i] );
            System.out.println ("*******************") ;
        }

        // Changing the outcome of the first toss from head to tail
        tossOutcome [ 0 ] = "Tail" ; 
        System.out.println ("After modification of the first outcome of toss, the array elements are as follows : " );
        for ( int i = 0 ; i < tossOutcome.length ; ++i )
        {
            System.out.println ("\nToss number : " +(i+1)  ) ;
            System.out.println ("Coin toss outcome : " +tossOutcome[i] );
            System.out.println ("*******************") ;
        }

    }
}

Output :