on Leave a Comment

Java Array (With Examples)

An array is a data structure, which stores related data items. An array is a group of contiguous data items that shares a common name. If we need a set of variables to store same type of data, instead of defining many variables, we can simply define an array. The individual value in array is known as element.

Advantage of Java Array

- Provides ability to develop concise and efficient program.
- It provides random access. We can access any data at any index.

Disadvantage of Java Array

- We need to know in advance the size of array.
- The memory allocated to array can not be increased or decreased dynamically.

Types of Java Array

- Single Dimensional Array
- Multi-Dimensional Array

Single Dimensional Array

A single dimensional array is array in which list of items is stored under a single name using one subscript.

Declaration of Single Dimensional Array

Syntax:

datatype[] array-name;
or
datatype array-name[];

Example:

int[ ] arr;
char [ ]arr;
short arr[ ];

Instantiation of Java Array

Example:

float []arr = new float[20];

Accessing Array Element

Array index start from 0. We can access array element using indexes. 

Syntax:

array-name[n-1];

Example:

float[] arr = {1.1, 2.2, 3.3, 4.4, 5.5};

If we want to access the 5th element of above array. Then we write following code.

System.out.println("Value of 5th Element is " + arr[4]);


Example of Single Dimensional Java Array

In this java program, we declare, instantiate and access single dimensional array.

public class OneDArray{
 public static void main(String[] args) {
  int[] arr; //Declaration
  arr = new int[5]; //Instantiation
  arr[0] = 10;
  arr[1] = 20;
  arr[2] = 30;
  arr[3] = 40;
  arr[4] = 50;

  for(int i = 0; i<arr.length; i++)
   System.out.println("Value of Element"+i+" is "+arr[i]);
 }
}


Output:

Value of Element0 is 10
Value of Element1 is 20
Value of Element2 is 30
Value of Element3 is 40
Value of Element4 is 50

Multi-Dimensional Array

A multi-dimensional array can have multiple rows and multiple columns. 

Declaration of Multi-Dimensional Array

Syntax:

dataType[][] array-name; or   
dataType array-name[][]; or  
dataType []array-name[];

Instantiation of Multi-Dimensional Array

Example:

int[][] arr=new int[5][5]; 
float array-name[5][5]; or  
char [5]array-name[5];

Initialization of Multi-Dimensional Array

Example:

arr[0][0]=1;  
arr[0][1]=2;  
arr[0][2]=3;  
arr[0][3]=4;  
arr[0][4]=5;  

Example of Multi-Dimensional java array

public class MultiDArray{
 public static void main(String[] args) {
  int[][] arr = new int[3][3]; //Declaring and Instantiating
  arr[0][0]=1;  //Initializing
  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; 
  for (int i = 0; i<arr.length ; i++) {
   for (int j = 0; j<arr.length; j++) {
    System.out.print(arr[i][j]+"\t");
   }
   System.out.println();
  }
 }
}

Output:

1       2       3
4       5       6
7       8       9



0 comments:

Post a Comment