Java – Array

array
Java – Array Introduction

Array is a Non-Primitive Data for Java. Java’s array contains many elements of the same data type. But these elements are in their nearby memory locations.

Java Array keeps these variables of primitive data types inside.

Array is an object. Which holds the same primitive data type variables. Array’s size is fixed.

 Length of Array is 5
Array_Elements     
Array_index01234

Array has three methods.

  1. Array Declaration
  2. Creating Array
  3. Array Initialization

Syntax for Array Declaration

The declaration of Array is done in two ways.

data_type array_name []; // or
data_type [] array_name;

The first type in Syntax is the one used for java.

There is another type in Syntax which is used in C and C ++. It also works in java, but is not used.

Example for Array Declaration

When declaring an Array, its size is not given in its subscript ([]).

int [] arr;
char [] str;
double [] arr;

Syntax for Creating Array

data_type [] array_name = new data_type [array_size]; // declaring and creating Array

Example for Creating Array

The new operator is used when Array is to be created. Here the size of the array is given.

In the following example, two types of array named ‘arr’ are declare and create.

In the first type ‘arr’ this array has also been created with declaration.

In another type, ‘arr’ has declared this array first and subsequently it has been created.

int [] arr = new int [10]; // declaring and creating Array
						 // or
int [] arr; // declaring array
arr = new int [10]; // Creating Array
 Length of Array is 10
Array_Elements          
Array_indexarr [0]arr [1]arr [2]arr [3]arr [4]arr [5]arr [6]arr [7]arr [8]arr [9]
 Array name is ‘arr’

Syntax for Array Initialization

data_type [] array_name = {array_elements};

Here the array is initialized in a single line. The initialization of this array does not require a new operator. As values ​​are given in elements. As such, the size of the array also increases.

Example for Array Initialization

int [] arr = {1, 2, 3, 4, 5};

Here the array is initialized seperatly.

Example for Array Initialization

arr [0] = 1; // Array initialization index by index
arr [1] = 2;
arr [2] = 3;
arr [3] = 4;
arr [4] = 5;

Accessing Array Elements

Array’s elements are accessed in two ways.

  1. By indexing
  2. By for or for each loop
1. By Indexing: Accessing Array Elements

For Example, Source Code:

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

	int [] arr = {1, 2, 3, 4, 5}; // Array initialization
	
	System.out.println ("arr [0] =" + arr [0]);
	System.out.println ("arr [1] =" + arr [1]);
	System.out.println ("arr [2] =" + arr [2]);
	System.out.println ("arr [3] =" + arr [3]);
	System.out.println ("arr [4] =" + arr [4]);
}
}

Output:

arr [0] = 1
arr [1] = 2
arr [2] = 3
arr [3] = 4
arr [4] = 5
1. By for or for each Loop: Accessing Array Elements
Using For Loop

For Example,
Source Code:

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

	int [] arr = {1, 2, 3, 4, 5}; // Array initialization
	int i;
	
	for (i = 0; i

Output :

arr [0] = 1
arr [1] = 2
arr [2] = 3
arr [3] = 4
arr [4] = 5
Using For each Loop

For Example,
Source Code:

class Sample {

	public static void main (String args []) {
		
	int [] arr = {1, 2, 3, 4, 5};

	for (int i: arr) {
		
		System.out.println ("arr [" + (i-1) + "] =" + i);

	}
}
}

Output:

arr [0] = 1
arr [1] = 2
arr [2] = 3
arr [3] = 4
arr [4] = 5
Java - Types of Array

There are two types of array.

  1. One-dimensional dimensions
  2. Two-Dimensional or Multi-Dimensional Array

1. One-Dimensional Array

Array Declaration, Creating Array, Array Initialization and Accessing Array on Array Introductions are all of ‘One Dimensional Array’. You can read it.

2. Two-Dimensional or Multi-Dimensional Array

Multi-Dimensional Array Declaration

int arr [] []; // or
int [] [] arr; // or
int [] arr [];

Create Multi-Dimensional Array

int arr [] [] = new int [2] [2]; // or
int [] [] arr = new int [2] [2]; // or
int [] arr [] = new int [2] [2];

Multi-Dimensional Array Initialization

int arr [] [] = {{1, 2}, {3, 4}};

Elements of Multi-Dimensional Array are accessed in two ways.

  1. By indexing
  2. By for loop
1. By Indexing: Accessing Array Elements

For Example, Source Code:

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

	int [] [] arr = {{1, 2}, {3, 4}}; // Array initialization
	
	System.out.println ("arr [0] [0] =" + arr [0] [0]);
	System.out.println ("arr [0] [1] =" + arr [0] [1]);
	System.out.println ("arr [1] [0] =" + arr [1] [0]);
	System.out.println ("arr [1] [1] =" + arr [1] [1]);
}
}

Output:

arr [0] [0] = 1
arr [0] [1] = 2
arr [1] [0] = 3
arr [1] [1] = 4
1. By for Loop: Accessing Array Elements Using For Loop

For Example,
Source Code:

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

	int [] [] arr = {{1, 2}, {3, 4}}; // Array initialization
	int i, j;
	
	for (i = 0; i

Output :

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