X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Java-Array :

An array is an indexed collection of fixed number of homogeneous data elements.

The main advantage of array is that we can represent multiple values under the same name. So, the readability of the code improved.

But the main limitation of array is once we created an array, there is no chance of increasing/decreasing the size based on our requirement. Hence, in memory point of view arrays concept is not recommended to use.

We can resolve this problem by using Collections.


Array Declaration :

Single Dimension Array Declaration :-

  • int[ ] a;




  • int a[ ];




  • int [ ]a;




First one is recommended because the type is clearly separated from the name.

At the time of declaration we can't specify the size.

Example :

int[10] a; // invalid


Two - Dimension Array Declaration :-

  • int[ ][ ] a;




  • int a[ ][ ];




  • int [ ][ ]a;




  • int[ ] a[ ];




  • int[ ] [ ]a;




  • int [ ]a[ ];





Three - Dimension Array Declaration :-

  • int[ ][ ][ ] a;




  • int a[ ][ ][ ];




  • int [ ][ ][ ]a;




  • int[ ] a[ ][ ];




  • int[ ] [ ][ ]a;




  • int[ ] [ ]a[ ];




  • int[ ][ ] [ ]a;




  • int[ ][ ] a[ ];




  • int [ ][ ]a[ ];




  • int [ ]a[ ][ ];





Array Construction :

Every array in Java is an object. Hence, we can create it by using new operator.

Example :

int[ ] a = new int[10];

For every array type corresponding classes are available. These classes are not applicable for programmer level.

At the time of construction of array, it is compulsory we should specify the size.

It is legal to have an array with size 'zero' in Java.

Example :

int[ ] a = new int[0]; System.out.println(a[0]); // Runtime Exception : ArrayIndexOutOfBoundsException: 0

If we are specifying array size as negative integer value, we will get runtime exception saying NegativeArraySizeException.

Example :

int[ ] a = new int[-1]; // Runtime Exception : NegativeArraySizeException

To specify array size the allowed data types are byte, short, int, char.

Example :

int[ ] a = new int['A']; System.out.println(a[64]); // Output : 0 System.out.println(a[65]); // Runtime Exception : ArrayIndexOutOfBoundsException: 65


Note :

The maximum allowed array size in Java is 2147483647.


Creation of Multi - Dimensional Array :

In Java multi - dimensional arrays are not implemented in matrix form. They implemented using 'Array of Array' concept.

The main advantage of this approach is memory utilization will be improved.

X

utilization :

It refers to making practical and effective use of (something).

Example 1 :

int[ ][ ] a = new int[3][ ]; a[0] = new int[2]; a[1] = new int[1]; a[2] = new int[3];

Example 2 :

int[ ][ ][ ] a = new int[2][ ][ ]; a[0] = new int[3][ ]; a[0][0] = new int[1]; a[0][1] = new int[2]; a[0][2] = new int[3]; a[1] = new int[2][2];


Array Initialization :

Whenever we are creating an array automatically every elements are initialized with default values.

Example :

class example { public static void main(String [ ] args) { int[ ] a = new int[3]; System.out.println(a); System.out.println(a[0]); } }

Output :

[I@230e2610 0

Note :

Whenever we are trying to print any object reference internally toString() will be called which is implemented as follows :

classname@hexadecimal_string_of_hashcode

Example 1 :

class example { public static void main(String [ ] args) { int[ ][ ] a = new int[3][3]; System.out.println(a); System.out.println(a[0]); System.out.println(a[0][0]); } }

Output :

[[I@75e4f66a [I@aede59e 0

Example 2 :

class example { public static void main(String [ ] args) { int[ ][ ] a = new int[3][ ]; System.out.println(a); System.out.println(a[0]); System.out.println(a[0][0]); } }

Output :

[[I@75e4f66a null NullPointerException

Once we created an array, every element by default initialized with default values. If we are not satisfy with those default values then we can override those with our customized values.

Example :

int[ ] a = new int[5]; a[0] = 10; a[1] = 20; a[3] = 40;

Note :

If we are trying to access an array with out of range index we will get runtime exception - ArrayIndexOutOfBoundsException "AIOBE".


Array Declaration, Construction & Initialization in a single line :

We can declare, construct & initialize an array in a single line.

Example :

int[ ] a = {10 , 20 , 30 , 40}; char[ ] ch = {'a' , 'e' , 'i' , 'o' , 'u'}; String[ ] s = {"John" , "Jack"}; We can extend this short-cut even for multi - dimensional arrays also. int[ ][ ] a = {{10 , 20 , 30} , {40 , 50 ,60}}; int[ ][ ][ ] a = {{{10 , 20} , {30 , 40} , {50 , 60}} , {{70 , 80} , {90 , 100} , {110 , 120}}};


length :

It is final variable applicable only for arrays. It represents the size of array.

Example :

class example { public static void main(String [ ] args) { int[ ] a = new int[10]; System.out.println(a.length); } }

Output :

10

In multi - dimensional arrays length variable represents only base size but not total size.

Example :

class example { public static void main(String [ ] args) { int[ ][ ] a = new int[6][4]; System.out.println(a.length); System.out.println(a[0].length); } }

Output :

6 4

Note :

length variable is applicable only for arrays where as length() is applicable for String objects.



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.