Arrays
An array can be defined as a collection of similar elements. Java supports three types of arrays namely
- Single dimensional.
- Double dimensional.
- Multi dimensional.
A single dimensional array can be created in the following format.
Syntax:data_type array_name[] = new data_type[size];
- (or)
data_type array_name[];
class App10{
} |
- output:
- \>javac App10.java
- \>java App10
- The elements in the array are ………
- 10
- 20
- 30
- 40
-
- Length property: Whenever you create an array in Java, automatically a special property called length is attached to it. The length property returns the size of the array.
- Example: for(i=0; i<x.length; i++)
- Length property: Whenever you create an array in Java, automatically a special property called length is attached to it. The length property returns the size of the array.
A double dimensional array is a matrix of rows and columns. A two dimensional array can be declared in the following format…
Syntax:data_type array_name[][]=new data_type array_name[rows][columns];
- (or)
data_type array_name[][];
class App11{
} |
- output:
- \>javac App11.java
- \>java App11
- The elements in the Matrix are ………
- 1 0 0
- 0 1 0
- 0 0 1
class App12{
} |
- output:
- \>javac App12.java
- \>java App12
- The elements in the Matrix are ………
- 1 2 3
- 4 5 6
- 7 8 9
A multidimensional array can be declared in the following format…..
Syntax:data_type array_name[][][]=new data_type array_name[x][y][z];
- or
data_type array_name[][][];
- array_name = new data_type array_name[x][y][z];
Note: Java allows you to create a variety of arrays with a fixed no. of rows and each row varies with different no. of cell’s. Example: App13.java
class App13{
} |
- output:
- \>javac App13.java
- \>java App13
- The elements in the Matrix are ………
- 1 2 3 4 5
- 6 7
- 8 9 10 11 12 13 14 15
