Arrays
Arrays
—--------
Array is an object, it is able to store the same type of elements as per
indexing, where the index value will start from 0 and it will end with
size-1.
Syntax:
DataType[] refVar = {val1, val2,....,val_n};
EX: int[] a = {10,20,30,40,50};
Syntax:
DataType[] refVar = new DataType[size];
refVar[0] = val1;
refVar[1] = val2;
—--
—---
refVar[size-1] = val-n;
EX:
int[] a = new int[5];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;
[[...Letter@refVal
After declaring and initializing the array in the above approaches, we have
to access the data from the array by using the following syntax.
In the above syntax, if we provide the index value which is in the outside
range of the array indexes then JVM will raise an exception like
java.lang.ArrayIndexOutOfBoundsException.
int[] a = {10,20,30,40,50};
Sopln(a[0]);
Sopln(a[4]);
Sopln(a[10]); —> java.lang.ArrayIndexOutOfBoundsException
To read all elements from the array in the above approach we have to use the
following instructions
int[] a = {10,20,30,40,50};
Sopln(a[0]);
Sopln(a[1]);
Sopln(a[2]);
Sopln(a[3]);
Sopln(a[4]);
In the above case we are writing duplicate code every time, here to reduce
duplicate code we have to use loops.
If we want to read elements from the array then we have to use a for loop.
EX:
int[] a = {10,20,30,40,50};
for(int index = 0; index < a.length; index++){
System.out.println(a[index]);
}
Index: OP
0 10
1 20
2 30
3 40
4 50
To read elements from the array if we use the above approach then we are able
to get the following problems.
1. We have to manage a separate variable for looping purposes.
2. We have to execute a conditional expression at each and every
iteration, it will take more memory and more execution time, because
conditional expressions are more strengthful expressions.
3. We have to perform increment / decrement operations over a loop
variable at each and every iteration.
4. We are providing index values explicitly to the array inorder to get
array elements, here if the index value is not proper then there is a
chance to get ArrayIndexOutOfBoundsException.
All the above problems are able to reduce java applications performance.
To overcome all the above problems, JDK1.5 version has provided an
enhancement in the for loop that is for-Each loop.
Syntax:
for(ArrayDataType var: ArrayRefVar){
—-----
}
In the case of for-Each loop, JVM is able to read element by element from the
array and assign element by element to the variable in the forEach loop and
it will execute the loop body for each and every element.
EX:
int[] a = {10,20,30,40,50};
for(int x: a){
Sopln(x);
}
EX:
public class Main {
public static void main(String[] args){
//int[] a = {10,20,30,40,50};
int[] a = new int[5];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;
System.out.println(a);// [I@abc123
System.out.println(a.length);// 5
System.out.println(a[2]);// 30
System.out.println(a[4]);// 50
//System.out.println(a[5]);ArrayIndexOutOfBoundsExc
eption
//System.out.println(a[a.length]);
ArrayIndexOutOfBoundsException
//System.out.println(a[a.length-6]);
ArrayIndexOutOfBoundsException
System.out.println(a[a.length-2]);// 40
System.out.println();
for(int index = 0; index < a.length; index++){
System.out.println(a[index]);
}
System.out.println();
for (int element: a){
System.out.println(element);
}
}
}
[I@65ab7765
5
30
50
40
10
20
30
40
50
10
20
30
40
50
Multi Dimensional Arrays:
—-------------------------
Multi Dimensional Arrays are able to represent data in more than one
dimension or level.
Syntax:
DataType[][][]...[] refVar = {{{...{val1,val2,...val-n},...}...},...};
EX:
int[][] a = {{1,2,3},{2,3,4},{3,4,5}};
Int[][][] a = {{{1,2,3},{2,3,4}},{{3,4,5},{4,5,6}},{{5,6,7},{6,7,8}}}
EX:
int[][] a = new int[3][3];
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 2;
a[1][1] = 3;
a[1][2] = 4;
a[2][0] = 3;
a[2][1] = 4;
a[2][2] = 5;
EX:
public class Main {
public static void main(String[] args) {
int[][] a = {{1,2,3},{2,3,4},{3,4,5}};
System.out.println(a);
System.out.println(a.length);
System.out.println(a[0]);
System.out.println(a[2]);
//System.out.println(a[3]); -->
ArrayIndexOutOfBoundsException
System.out.println(a[1].length);
System.out.println(a[1][1]);
System.out.println(a[2][1]);
//System.out.println(a[1][3]);-->
ArrayindexOutOfBoundsException
System.out.println(a[a.length-1][a.length-2]);//
a[2][1]
System.out.println(a[a[2].length-1][a[1].length-2]);//
a[2][1]
System.out.println();
}
}
[[I@65ab7765
3
[I@1b28cdfa
[I@eed1f14
3
3
4
4
4
1 2 3
2 3 4
3 4 5
1 2 3
2 3 4
3 4 5
In Java applications, we are able to declare arrays for the user defined data
types like Classes, abstract classes, interfaces….
EX:
Course.java
package com.durgasoft.entities;
Main.java
import com.durgasoft.entities.Course;
import com.durgasoft.entities.Student;
Anonymous Arrays:
—-----------------
Anonymous arrays are the nameless arrays, they will be used to pass arrays as
parameters to the methods without declaring arrays.
Syntax:
new DataType[]{val1, val2, val3,....};
EX:
Bank.java
package com.durgasoft.entities;
Note: IN general, in java applications anonymous arrays are suitable for the
less number of values inside the array, if we have more values inside the
array it is suggestible to use explicit arrays.
Q)Find the valid syntaxes from the following list of arrays declaration?
—------------------------------------------------------------------------
Ans:
—---
1. int[] a = {10,20,30,40}; —--> Valid
2. int[3] a = {10,20,30}; —----> Invalid
3. int a[] = {10,20,30,40};----> Valid