Declaration
of array
A
Java array variable is declared just like, you would declare a
variable of the desired type, except you add []
after the type. Here is a simple Java array declaration example:
int[]Array;
So, it follows the
structure:
dataType[]ArrayName;
These are some more
examples:
double[]
List;
String[]
Names;
int[]
Record;
When
you declare a Java array variable you only declare the variable
(reference) to the array itself. The declaration does not actually
create an array. To
create an array,
following the declaration, the following format is used:
dataType[]ArrayName;
ArrayName = new
dataType[size];
The following syntax can
also be applied to declare and create and array:
dataType[]ArrayName =
new dataType[size];
For example,
String[]
Names = new String[7];
int[]
Marks = new int[9];
This method is simpler and
more concise.
After this, the array can
be initialized by
storing the elements:
The
syntax is for this is,
dataType[]
ArrayName =
{element1,
element2...};
For
example
double[]
List =
{1.9,
2.9,
3.4,
3.5};
Or it can be done in this
way:
dataType[]
ArrayName = new dataType[size];
ArrayName[0]=element1;
...
ArrayName[size-1]=elementsize;
For example,
int a[]=new int[5];
a[0]=10;
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
The position in this case
can be given in three ways:
Integer constant
E.g.
a[2]=4;
Integer variable
E.g.
int
x=5;
a[x]=3;
Arithmetic operation
E.g.
int
x=5;
a[x+1]=3;
Types
of arrays
There are
two types of arrays in Java based on dimensions:
Single-Dimensional
Array
Multi-Dimensional
Array
Single-Dimensional
Array
A Single-Dimensional
Array is a structured collection of
components (often called array elements) that can be accessed
individually by specifying the position of a elements with a single
index value. They have only one subscript value and store data in a
linear form.
An example
of a Single-Dimensional
Arrays is a list.
The syntax
for such an array is the same as has been described in the
Declaration of an array section. It is as follows:
The first method is:
Declaration->
dataType[]
ArrayName =
{e1,
e2........};
Example
int[] scores
= {5, 10, 20, 50};
The other
method is:
dataType[]ArrayName; ->
Declaration
ArrayName = new
dataType[size]; -> Creation of array
ArrayName[0]=element1;
.................
.................
ArrayName[size-1]=elementsize;
Example:
String[]
Employees;
Employees =
new String[2];
Employees[0]=”Rahul”;
Employees[1]=”Manish”;
1D-
Array Example:
class example1d
{
public static void main(String args[])
{
int m[] = new int[5];
m[0] = 50;
m[1] = 60;
m[2] = 70;
m[3] = 80;
m[4] = 90;
System.out.println("The marks for 5 students are:");
for(int i = 0; i < 5; i++)
{
System.out.print(+m[i]);
System.out.println();
}
}
}
Multi-dimensional
arrays
An array
that has more than 1 subscript is known as a multi-dimensional array.
A one-dimensional array stores data in a linear form however a
multi-dimensional array can store more information. So it can be
thought of as an array of one-dimensional arrays.
Note: The
most common example of a one-dimensional array is a list,
the primary example of a multi-dimensional array is a table.
The
syntax for multi-dimensional arrays is:
dataType [ ][ ]
ArrayName = new dataType[][];
This will create a
two-dimensional array.
Note: With each “[]”
added, the number of dimensions increases by one.
For example,
int [ ][ ][ ] 3d
= new int[4][3][5];
This
creates a
three-dimensional array of integer values.
Method to Define 2D
Array:
int [ ][ ] 2d = new
int[2][3];
2d[0][0] = 30;
2d [0][1] = 40;
2d [0][2] = 50;
2d
[1][0] = 10;
2d [1][1] = 20;
2d [1][2] = 30;
The
output is:
30
40 50
10
20 30
Another
way to represent 2D Array:
int[][]
2d = { { 30, 40, 50 },
{
10, 20, 30 }
};
The output
is the same as above.
Example
program for multi-dimensional array:
class multidexample
{
public static void main(String[] args)
{
int[][] a = {{2,3,4},{1,3,5},{9,7,5}
};
int[][] b = {{70,80,90},{56,38,59},{19,27,15}
};
int[][] c = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Sum of the two matrices is");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}