Java Arrays

Java supports an information structure, which is similar to a cluster. This information structure is called an array. It is capable of storing an altered size successive accumulation of components of the same data type. An array is utilized to store an accumulation of information, yet it is frequently more valuable to think about it as an exhibit for storing variables of the same sort.

As opposed to making declarations of individual variables, for example, num0, num1 and num99, you can declare one array variable. For example, an array of four elements is declared as arrayname[4]. This chapter discusses all the facets of array declaration, access and manipulation.

How To Declare array Variables

To utilize an array as a part of a system, you must declare a variable to reference the array. Besides this, you must determine the sort of array the variable can reference. Here is the syntax for declaring a variable of the type array:

datatype[] myarray;

Types of Array in java

There are two types of array.

  • Single Dimensional Array
  • Multidimensional Array

One-Dimensional Arrays

The general form of a one-dimensional array declaration is

type var-name[];
OR
type[] var-name;

An array declaration has two components: the type and the name. type declares the element type of the array. The element type determines the data type of each element that comprises the array. Like an array of integers, we can also create an array of other primitive data types like char, float, double, etc., or user-defined data types (objects of a class). Thus, the element type for the array determines what type of data the array will hold. 

Example: 

// both are valid declarations
int intArray[];
or int[] intArray;

byte byteArray[];
short shortsArray[];
boolean booleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];

// an array of references to objects of
// the class MyClass (a class created by
// user)
MyClass myClassArray[];

Object[] ao, // array of Object
Collection[] ca; // array of Collection
// of unknown type

Although the first declaration establishes that int Array is an array variable, no actual array exists. It merely tells the compiler that this variable (int Array) will hold an array of the integer type. To link int Array with an actual, physical array of integers, you must allocate one using new and assign it to int Array.

Instantiating an Array in Java

When an array is declared, only a reference of an array is created. To create or give memory to the array, you create an array like this: The general form of new as it applies to one-dimensional arrays appears as follows: 

var-name = new type [size];

Here, type specifies the type of data being allocated, size determines the number of elements in the array, and var-name is the name of the array variable that is linked to the array. To use new to allocate an array, you must specify the type and number of elements to allocate.

Example: 

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

OR 

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


Multidimensional Array in Java

In such case, data is stored in row and column based index (also known as matrix form).

Syntax to Declare Multidimensional Array in Java

dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];

Example to instantiate Multidimensional Array in Java

int[][] arr=new int[3][3];//3 row and 3 column  

Example to 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;