What is an Array?
name [ i ] where 'i' is the subscript/index with range from 0 to 2.
int[ ] numArray = new int[3];
numArray[0]= 1;
numArray[1]= 2;
numArray[2]= 3;
String Array
String[ ] nameArray = new String[3];
nameArray[0]= "Village";
nameArray[1]= "Town";
nameArray[2]= "City";
Boolean Array
Boolean[ ] tfArray = new Boolean[3];
tfArray[0]= false;
tfArray[1]= true;
tfArray[2]= false;
Note : 'false' and 'true' are not within quotes.
Initializing the array with list of values.
int[ ] numArray;
numArray = new int[ ]{1,2,3,4,5};
String[ ] nameArray;
nameArray = new[ ]{"Village","Town","City"}
Boolea[ ] tsArray;
tfArray = new[ ]{true,false,true};
Note:
size is not mentioned within square brackets, size is implicitly created by no. of elements.
int[ ] numArray;
numArray =new int[10];
int i=0;
for (i=0;i<numArray.length;++i){
numArray[i]=i;
}
First element in the Array
Index of an array object starts with 0,not 1.
So,first element in an array is numArray[0];
Last element in the Array
(Length of array -1) gives the last element in an array.
So, last element in the array is numArray[numArray.length - 1];
That is why in the FOR loop, maximum value of the index 'i' is
less than length of the array.
If,it is equal to array length ,out of bound exception will be raised.
An array is a single
variable or a data structure that can
store more than one value
of similar types at a time.
Normal variables can
hold only one value at a time.
Supposing I want to
store three strings ‘village’, ’town’, ‘city’
Normally I need three variables
with three different names as below
name1
=”village”, name2 =”town” and name3 = “city”.
If I use an array
variable, with one variable name, three values can be stored.
Those three values can be accessed individually by changing the subscript 'i'
from 0 to 2.
Those three values can be accessed individually by changing the subscript 'i'
from 0 to 2.
name [ i ] where 'i' is the subscript/index with range from 0 to 2.
name [0] = “Village”,
name [1] = “Town”,
name [2] = “City”
name [1] = “Town”,
name [2] = “City”
This is similar to numbering of hotel rooms.
Hotel rooms numbered serially.
So, Hotel Name and room
number is enough to address a person staying in a particular room.
Here, hotel name is the
variable and room number can be the Value or index itself.
Array in Java
An array in Java is an
object, it is not like other primitive types such as int, float etc.
So, an array cannot be declared
like other primitives types.
It has to be declared and instantiated in
the same way as other class objects before accessing it.
Arrays are multidimensional.
That is, one can have single dimension (Vector), 2 dimension (Matrix),
3 dimension (3D Tensor) or even n-Dimension(ND-Tensor).
Array - Declaration and Instance Creation
Arrays in Java are declared as below.
int[ ] numArray = new int[3];
We will see them through a hotel example.
We have four hotels.
Example-1 Hotel 1 has only ground floor (Floor 0) with rooms. (1D)
Example-2 Hotel 2 has Floor 0 and 1 with same number of rooms.(2D)
Example-3 Hotel 3 has Floor 0 and 1 with different number of rooms (2D Skewed)
Example-4 Hotel 3 has 2 Blocks, each block has 0+1 floors with same number of rooms.(3D)
Example -1 for one dimension array ( Hotel with floor 0 and rooms)
package hotel1;
public class Hotel1 {
public static void main(String[] args) {
/* Array object declaration,Instant creation & for memory allocation, size must be specified. */
int[ ] hArr;
hArr =new int[3];
int i=0; //subscript and also the RoomNumber
int hArrSize=0;
/* "length" PROPERTY of Array gives the size, it is not a METHOD */
hArrSize = hArr.length;
/* Last element is at (size - 1) So,it must be i<hArrSize,not i=hArrSize */
for (i=0;i<hArrSize;++i){
hArr[i]=i+1;
}
/* Display of array elements (Room Numbers) */
System.out.print("Rooms in floor[0]: ");
for (i=0;i<hArr.length;++i){
System.out.print(hArr[i]+",");
}
}
}
Output:
Example -2 for two dimension array ( Hotel with floor 0 ,1 and rooms)
package hotel2;
public class Hotel2 {
public static void main(String[] args) {
/* Two square brackets are used to indicate 2 dimensional array */
int[ ][ ] hfArr= new int[2][3];
int row=0,col=0,roomNo=1;
/* Each element of first array refers to second array.
Length of array (hfArr.length) gives number of rows.
For each ROW,number of columns is given by (hfArr[row].length) */
for(row=0;row<hfArr.length;row++){
for(col=0;col<hfArr[row].length;col++){
hfArr[row][col]=roomNo;
roomNo++;
}
}
/* Display of array elements (Floors & Room Numbers) */
for(row=0;row<hfArr.length;row++){
System.out.print("Room Numbers in Floor:["+(row)+"] ");
for(col=0;col<hfArr[row].length;col++){
System.out.print(hfArr[row][col]+" ");
}
System.out.println("");
}
}
}
Output:
run:
Room Numbers in Floor:[0] 1 2 3
Room Numbers in Floor:[1] 4 5 6
Example -3 Skewed two dimension array ( Hotel with floor 0 ,1 with different number of rooms)
package hotel4;
public class Hotel4 {
public static void main(String[] args) {
/* Three square brackets are used to indicate 3 dimensional array */
int[ ][ ][ ] hfArr= new int[2][2][2];
int l=0,b=0,h=0,roomNo=1;
/* Looping is similiar to 2D array but at three levels */
for(l=0;l<hfArr.length;l++){
for(b=0;b<hfArr[l].length;b++){
for(h=0;h<hfArr[l][b].length;h++){
hfArr[l][b][h]=roomNo;
roomNo++;
}
}
}
for(l=0;l<hfArr.length;l++){
System.out.print("Block:["+(l+1)+"]\n");
for(b=0;b<hfArr[l].length;b++){
System.out.print("Room Numbers in Floor:["+(b)+"] ");
for(h=0;h<hfArr[l][b].length;h++){
System.out.print(hfArr[l][b][h]+" ");
roomNo++;
}
System.out.println("");
}
}
}
}
Output:
run:
Block:[1]
Room Numbers in Floor:[0] 1 2
Room Numbers in Floor:[1] 3 4
Block:[2]
Room Numbers in Floor:[0] 5 6
Room Numbers in Floor:[1] 7 8
Important Points:
Arrays are multidimensional.
That is, one can have single dimension (Vector), 2 dimension (Matrix),
3 dimension (3D Tensor) or even n-Dimension(ND-Tensor).
Array - Declaration and Instance Creation
Arrays in Java are declared as below.
int[ ] numArray = new int[3];
String[ ] nameArray = new String[3];
Boolean[ ] tfArray = new Boolean int[3];
Boolean[ ] tfArray = new Boolean int[3];
Left Side
Declaration of an array begins with type of variable followed by square brackets.
Square brackets indicate that the variable is an array, not a normal primitive type
followed by user defined array name.
Right Side
followed by user defined array name.
Right Side
The key word NEW creates and allocates memory for the integer array object.
Since memory is allocated, size of the array must be given.Here it is 3.
Since memory is allocated, size of the array must be given.Here it is 3.
This shows, array in Java is static and not dynamic; its size cannot be changed during run time.
Array elements are allocated contiguous memory space, this enables faster access to elements using index.
We will see them through a hotel example.
We have four hotels.
Example-1 Hotel 1 has only ground floor (Floor 0) with rooms. (1D)
Example-2 Hotel 2 has Floor 0 and 1 with same number of rooms.(2D)
Example-3 Hotel 3 has Floor 0 and 1 with different number of rooms (2D Skewed)
Example-4 Hotel 3 has 2 Blocks, each block has 0+1 floors with same number of rooms.(3D)
Example -1 for one dimension array ( Hotel with floor 0 and rooms)
package hotel1;
public class Hotel1 {
public static void main(String[] args) {
/* Array object declaration,Instant creation & for memory allocation, size must be specified. */
int[ ] hArr;
hArr =new int[3];
int i=0; //subscript and also the RoomNumber
int hArrSize=0;
/* "length" PROPERTY of Array gives the size, it is not a METHOD */
hArrSize = hArr.length;
/* Last element is at (size - 1) So,it must be i<hArrSize,not i=hArrSize */
for (i=0;i<hArrSize;++i){
hArr[i]=i+1;
}
/* Display of array elements (Room Numbers) */
System.out.print("Rooms in floor[0]: ");
for (i=0;i<hArr.length;++i){
System.out.print(hArr[i]+",");
}
}
}
Output:
Rooms in floor[0]: 1,2,3
package hotel2;
public class Hotel2 {
public static void main(String[] args) {
/* Two square brackets are used to indicate 2 dimensional array */
int[ ][ ] hfArr= new int[2][3];
int row=0,col=0,roomNo=1;
/* Each element of first array refers to second array.
Length of array (hfArr.length) gives number of rows.
For each ROW,number of columns is given by (hfArr[row].length) */
for(row=0;row<hfArr.length;row++){
for(col=0;col<hfArr[row].length;col++){
hfArr[row][col]=roomNo;
roomNo++;
}
}
/* Display of array elements (Floors & Room Numbers) */
for(row=0;row<hfArr.length;row++){
System.out.print("Room Numbers in Floor:["+(row)+"] ");
for(col=0;col<hfArr[row].length;col++){
System.out.print(hfArr[row][col]+" ");
}
System.out.println("");
}
}
}
Output:
run:
Room Numbers in Floor:[0] 1 2 3
Room Numbers in Floor:[1] 4 5 6
Example -3 Skewed two dimension array ( Hotel with floor 0 ,1 with different number of rooms)
package hotel3;
public class Hotel3 {
public static void main(String[] args) {
int row=0,col=0,roomNo =1;
/* Among the two arrays, only one can be without size */
int skdArray [ ][ ]=new int[2][ ];
/* For each element of first array, different size is declared for the second array */
skdArray[0]= new int [4]; // Floor 0 with 4 rooms
skdArray[1]= new int [3]; // Floor 1 with 3 rooms
for(row=0;row<skdArray.length;row++){
for(col=0;col<skdArray[row].length;col++){
skdArray[row][col]=roomNo;
roomNo++;
}
}
for(row=0;row<skdArray.length;row++){
System.out.print("Room Numbers in Floor["+(row)+"] ");
for(col=0;col<skdArray[row].length;col++){
System.out.print(skdArray[row][col]+" ");
}
System.out.println();
}
}
Output:
run:
Room Numbers in Floor[0] 1 2 3 4
Room Numbers in Floor[1] 5 6 7
Example -4
Three dimension array ( Hotel with 2 Blocks, each with floor 0 ,1 with same number of rooms)
Output:
run:
Room Numbers in Floor[0] 1 2 3 4
Room Numbers in Floor[1] 5 6 7
Example -4
Three dimension array ( Hotel with 2 Blocks, each with floor 0 ,1 with same number of rooms)
package hotel4;
public class Hotel4 {
public static void main(String[] args) {
/* Three square brackets are used to indicate 3 dimensional array */
int[ ][ ][ ] hfArr= new int[2][2][2];
int l=0,b=0,h=0,roomNo=1;
/* Looping is similiar to 2D array but at three levels */
for(l=0;l<hfArr.length;l++){
for(b=0;b<hfArr[l].length;b++){
for(h=0;h<hfArr[l][b].length;h++){
hfArr[l][b][h]=roomNo;
roomNo++;
}
}
}
for(l=0;l<hfArr.length;l++){
System.out.print("Block:["+(l+1)+"]\n");
for(b=0;b<hfArr[l].length;b++){
System.out.print("Room Numbers in Floor:["+(b)+"] ");
for(h=0;h<hfArr[l][b].length;h++){
System.out.print(hfArr[l][b][h]+" ");
roomNo++;
}
System.out.println("");
}
}
}
}
Output:
run:
Block:[1]
Room Numbers in Floor:[0] 1 2
Room Numbers in Floor:[1] 3 4
Block:[2]
Room Numbers in Floor:[0] 5 6
Room Numbers in Floor:[1] 7 8
Important Points:
Array Size
Since size is declared during creation of array, it is available for use.
Array size can found by using "length" property of the array.
int arraySize;
arraySize = numArray.length; // 'length' is not a method but property
// So, no parenthesis like - length()
Since size is declared during creation of array, it is available for use.
Array size can found by using "length" property of the array.
int arraySize;
arraySize = numArray.length; // 'length' is not a method but property
// So, no parenthesis like - length()
Array Initialization
Arrays are initialized after creation with default values as
0 for integer type,
Null for object(String) type.
False for Boolean type,
They can be initialized with user values in the following ways.
Null for object(String) type.
False for Boolean type,
They can be initialized with user values in the following ways.
Initializing individual elements by assigning values.
Integer Array
Integer Array
numArray[0]= 1;
numArray[1]= 2;
numArray[2]= 3;
String Array
String[ ] nameArray = new String[3];
nameArray[0]= "Village";
nameArray[1]= "Town";
nameArray[2]= "City";
Boolean Array
Boolean[ ] tfArray = new Boolean[3];
tfArray[0]= false;
tfArray[1]= true;
tfArray[2]= false;
Note : 'false' and 'true' are not within quotes.
Initializing the array with list of values.
int[ ] numArray;
numArray = new int[ ]{1,2,3,4,5};
String[ ] nameArray;
nameArray = new[ ]{"Village","Town","City"}
Boolea[ ] tsArray;
tfArray = new[ ]{true,false,true};
Note:
size is not mentioned within square brackets, size is implicitly created by no. of elements.
Initializing the array using 'for' loop, if number of elements are large
int[ ] numArray;
numArray =new int[10];
int i=0;
for (i=0;i<numArray.length;++i){
numArray[i]=i;
}
First element in the Array
Index of an array object starts with 0,not 1.
So,first element in an array is numArray[0];
Last element in the Array
(Length of array -1) gives the last element in an array.
So, last element in the array is numArray[numArray.length - 1];
That is why in the FOR loop, maximum value of the index 'i' is
less than length of the array.
If,it is equal to array length ,out of bound exception will be raised.
for (i=0;i<numArray.length;++i){
numArray[i]=i;
}
2D Array - Difference type of Declaration and Instance Creation
int[ ] [ ] numd2Array= new int[2][3];
Here, int [ 2 ] [ 3 ] indicates that there are 2 arrays. Each element in the first array refers to second array of 3 elements. So, total number of elements is 6 overall.
Initializing with LIST
int[ ] num2dArray;
num2dArray = new int[ ] [ ]{{1,2,3},{4,5,6}};
Initializing with FOR loop
For the ROW,it is length of the 2D array (num2dArray.length)
and for each ROW, number of columns is given by the
length of each ROW of the array (num2dArray[row]).
int row=0,col=0,cellValue=0;
for(row=0; row < num2darray.length; row++){
for(col=0;col <num2darray[row].length; col++){
num2darray[row][col] = cellValue;
cellValue = cellValue+1;
}
}
numArray[i]=i;
}
2D Array - Difference type of Declaration and Instance Creation
int[ ] [ ] numd2Array= new int[2][3];
Here, int [ 2 ] [ 3 ] indicates that there are 2 arrays. Each element in the first array refers to second array of 3 elements. So, total number of elements is 6 overall.
Initializing with LIST
int[ ] num2dArray;
num2dArray = new int[ ] [ ]{{1,2,3},{4,5,6}};
Initializing with FOR loop
For the ROW,it is length of the 2D array (num2dArray.length)
and for each ROW, number of columns is given by the
length of each ROW of the array (num2dArray[row]).
int row=0,col=0,cellValue=0;
for(row=0; row < num2darray.length; row++){
for(col=0;col <num2darray[row].length; col++){
num2darray[row][col] = cellValue;
cellValue = cellValue+1;
}
}