Java Arrays

Article by: Manish Methani

Last Updated: October 25, 2021 at 2:04pm IST
3 min 16 sec read

Suppose you want to store the name of 100 students in memory. How will you achieve this? There are two ways :

1) Create 100 variables and assign a name to individual variables like a = Manish, b = Nitesh, c = Ram etc........

2) Create a single variable and give it the size of 100 elements. This is called an Array.

Java Array is used to store values in a memory of the same type at a time. Values of the same type mean either it stores values of int type or values of float type or char types at a time. You cannot store different types at a time in an array. int and float type data storage at the same time is not allowed when you initialise an array in Java.

Array Declaration in Java

The basic syntax for array declaration in Java is given below:-

type arrayName[];

Example:-

int student[];

Instantiate an Array

Now array is declared but the question is how much memory does that array needs. For that purpose, you need to instantiate an object to allocate memory using the new keyword. In the given syntactic example, student array requires a memory of 20 elements.

Syntax to allocate the memory for array elements :-

arrayRefVar = new dataType[arraySize];

Example:-

student = new int[20];

How to initialise an array in Java?

Syntax to initialise an array in Java is given below:-

Syntax : -

dataType[] arrayRefVar = {value0, value1, ..., valuek};

Example:-

public class javaArrayDemo{
  public static void main(String[] args)
  {
    int cityArray[]; //Array Declaration
   cityArray[] = new int[10]; //Allocate memory
    
   cityArray[0]  = 10;  //Initialise array
   cityArray[9]  = 20; 
   cityArray[5]  = 3; 

  System.out.println(cityArray[5]);
  }
}

Output:-

 3

Example:-

public class javaArraysDemo {
  public static void main(String[] args)
  {
    int cityArray[] = {10,20,30};   //Java initialise array
 
   System.out.println(cityArray[2]);
  }
}

Output :-

30

Looping through arrays

Array elements in Java can be accessed using basic while loop, for loop in Java.

Example:-

public class javaArraysDemo {
  public static void main(String[] args)
  {
    int cityArray[] = {10,20,30}; 
  
   for(int i =; i< cityArray.length; i++)
       System.out.println(cityArray[i]);
   }
}

Output :-

10
20
30

Enhanced Looping through arrays

Enhanced Looping is also called as for each loop. JDK 1.5 introduced a new for loop known as for each loop java or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.

Example :-

public class javaArraysDemo {
  public static void main(String[] args)
  {
    int cityArray[] = {10,20,30}; 
  
   for(int i : cityArray)
       System.out.println(i);
   }
}

Output :-

10
20
30

Java String Array

Below program will illustrate how to declare and initialise and iterate elements of the Java String Array.

import java.util.*;

class JavaArrayDemo
{
  public static void main (String[] args)
  {
   // String Array
   String[] arrayString = new String{"C", 
"O", "D", "Z", "I", 
"F", "Y"};

   // Displaying elements of String Array using basic for loop
   System.out.println("String Array using for loop :");
   for(int i = 0; i < arrayString.length; i++)
         System.out.printf("%s 	", arrayString[i]);

   // Displaying elements of String Array using for each loop
  System.out.printf("
String Array using for each loop :");
  for(String s : arrayString)
       System.out.printf("%s 	", s);

   // Displaying elements of String Array using while loop
    System.out.printf("
 String Array using while loop);
    int i = 0;
    while(i < arrayString.length)
    {
          System.out.printf(" %s 	", arrayString[i]);
          i++;
    }

    i = 0;
    // Displaying elements of String Array using do  while loop
      System.out.printf("String Array using do while loop :");
      do
      {
           System.out.printf("%s 	", arrayString[i]);
           i++;
      }while(i < arrayString.length);

  }
}

Output :-

String Array using for loop :
C  O  D  Z  I  F  Y
String Array using for each loop : C  O  D  Z  I  F  Y
String Array using while loop :   C  O  D  Z  I  F  Y
String Array using do while loop :   C  O  D  Z  I  F  Y

I hope this program will give you enough understanding about java string array and how to access elements of string array using for loop, for each loop, while loop and do while loop.

Firstly, we initialised a string array which contains string characters C, O, D, Z, I, F, Y. Then we applied basic looping techniques to print elements of the array. It's that simple.

Sorting an Array

sort method is used for an array sort. This program will illustrate the concept of java array sort.

import java.util.*;

class JavaArrayDemo
{
   public static void main (String[] args)
   {
     // String Array
    String[] arrayString = new String[] 
{"C", "O", "D", "Z",
 "I", "F", "Y" };

   // Sorting String Array
   Arrays.sort(arrayString);

 // Displaying elements of String Array using for loop
 System.out.println("Sorted String Array using for loop :");
 for (int i = 0; i < arrayString.length; i++)
       System.out.printf("%s 	", arrayString[i]);   
}
} 

Output:-

 Sorted String Array using for loop :
C D F I O Y Z

As you see initialised string array was in sequence C, O, D, Z, I, F, Y and after applying an array sort() method output is in alphabetical sequence order which is C, D, F, I, O, Y, Z

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com