« Previous Page Next Page »
    

Introduction to Java Array.

POSTED

AUTHOR: VARUN SINGH

In this tutorial, you will learn about Java Array, Java Array Variables, Java One Dimensional Array and Multidimensional Array step by step.


What is Java Array?

Normally, array is a collection of similar type of elements that have contiguous memory location. Once Array defined, the size of an array is fixed and can not increase to accommodate more elements. The first element of an array starts with index zero. It is a data structure where we store all elements of the same data type.


java array example

Types of Array in Java:

There are two types of Java Array used in Java Programming Language.

• Single Dimensional Array

• Multidimensional Array


Single Dimensional Array in java:

A Single Dimensional Array declaration has two components: the type and the name. The element type determines the data type of each element that comprises the array.

We can create an array of primitive data types like int, char, float, double etc or user defined data type(objects of a class). Thus, the element type for the array determines what type of data the array will hold.

Syntax to Declare and Instantiation an Array in java:

int intArray[];    //declaring array
intArray = new int[20];  // allocating memory to array

Combining both statements in one:

int[] intArray = new int[20]; // combining both statements in one

Example of Java Array:

//Java Program to declare, instantiate, initialize  Array
  
 class TestArray
{
    public static void main (String[] args) 
    {         
      // declares an Array of integers.
      int[] a;
         
      // allocating memory for 5 integers.
      a = new int[5];
         
      // initialize the first elements of the array
      a[0] = 10;
         
      // initialize the second elements of the array
      a[1] = 20;
         
      //so on...
      a[2] = 30;
      a[3] = 40;
      a[4] = 50;
         
      // accessing the elements of the specified array
      for (int i = 0; i < a.length; i++)
         System.out.println("Element at index " + i +  " : "+ a[i]);          
    }
}

Output:

Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50

Array of Object:

An array of objects is created just like an array of primitive type data items in the following way.

Student[] arr = new Student[8]; //student is a user-defined class

The student Array contains seven memory spaces of student class in which the address of seven Student objects can be stored.The Student objects have to be instantiated using the constructor of the Student class and their references should be assigned to the array elements in the following way.

Student arr = new Student[6];

Output: Creating Array of Objects:

// Java program to illustrate creating an array of objects 

class Student 
{ 
	public int roll_no; 
	public String name; 
	Student(int roll_no, String name) 
	{ 
		this.roll_no = roll_no; 
		this.name = name; 
	} 
} 

// Elements of array are objects of a class Student. 
 class StudentRecord 
{  
	public static void main (String[] args) 
	{ 
		

		// declares and allocating memory for 5 objects of type Student. 
		Student[] arr = new Student[5]; 

		// initialize the first elements of the array 
		arr[0] = new Student(1,"Varun"); 

		// initialize the second elements of the array 
		arr[1] = new Student(2,"Rohit"); 

		// so on... 
		arr[2] = new Student(3,"Vinay"); 
		arr[3] = new Student(4,"Vinit"); 
		arr[4] = new Student(5,"Mukesh"); 

		// accessing the elements of the specified array 
		for (int i = 0; i < arr.length; i++) 
			System.out.println("Element at " + i + " : " + 
						arr[i].roll_no +" "+ arr[i].name); 
	} 
}

Output:

Element at 0 : 1 Varun
Element at 1 : 2 Rohit
Element at 2 : 3 Vinay
Element at 3 : 4 Vinit
Element at 4 : 5 Mukesh

Multidimensional Arrays:

Multidimensional arrays are arrays of arrays with each element of the array holding the reference of other array. A multidimensional array is created by appending one set of square brackets ([]) per dimension.

int[][] intArray = new int[3][3]; // 3 row 3 column 2D array or matrix

int[][][] intArray = new int[3][3][3]; // 3D array

Initialize Multidimensional Array in Java

arr[0][0]=1;  
arr[0][1]=2;  
arr[0][2]=3;  
arr[1][0]=4;  
arr[1][1]=5;  
arr[1][2]=6;  
arr[2][0]=7;  
arr[2][1]=8;  
arr[2][2]=9;  

Example:

Example to declare, instantiate, initialize and print the 2 Dimensional array in java.

//Java Program to illustrate the use of multidimensional array  

class TestMultiArray{ 
public static void main(String args[])
{  

//declaring and initializing 2D array 
 
int arr[][]={{2,2,2},{3,3,3},{4,4,4}};  
  //printing 2D array  
   for(int i=0;i<3;i++)
   {  
      for(int j=0;j<3;j++)
	   { 
	     System.out.print(arr[i][j]+" ");  
       }  
      System.out.println();  
   }  
}
}  

Output:

222
333
444


The End:

Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

« Previous Page Next Page »

Comments: