X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Anonymous Array :

Sometimes we can create an array without any name also. Such type of nameless arrays are called anonymous arrays. The main objective of anonymous array is just for instant use.

We can create anonymous array as follows :

new int[ ] {10 , 20 , 30 , 40}

At the time of anonymous array creation we can't specify the size, otherwise we will get compile time error.

Example :

class example { public static void main(String [ ] args) { sum(new int[ ] {10 , 20 , 30 , 40}); } public static void sum(int[ ] x) { int total = 0; for(int y : x) { total = total + y; } System.out.println("The Sum : " + total); } }

Output :

The Sum : 100

Array element assignment :

Case 1 :

For the primitive type arrays as array elements we can provide any type which can be promoted to declare type.

Example :

For the int type arrays, the allowed element types are byte, short, char, int. If we are providing any other type then we will get compile time error. int[ ] a = new int[10]; a[0] = 10; a[1] = 'a'; byte b = 10; a[2] = b; short s = 20; a[3] = s; Note : For the float type array, the allowed element types are byte, short, char, int, long, float.


Case 2 :

In the case of object type arrays as array elements we can provide either declared type or its child class objects.

Example :

number[ ] n = new Number[10]; n[0] = new Integer(10); n[1] = new Double(10.5); n[2] = new String("John"); /* Compile time error : Incompatible types found : String require : Number */ Object[ ] a = new Object[10]; a[0] = new Object(); a[1] = new Integer(10); a[2] = new Double(10.5); a[3] = new String("John");


Case 3 :

In the case of abstract class type arrays as array elements we can provide its child class objects.

Example :

number[ ] n = new Number[10]; n[0] = new Integer(10);


Case 4 :

In the case of Interface type array as array element we can provide its implementation class objects.

Example :

Runnable[ ] r = new Runnable[10]; r[0] = new Thread(); r[1] = new String("John"); /* Compile time error : Incompatible types found : String require : Runnable */


Array variable assignment :

Case 1 :

Element level promotions are not applicable at array level.

Example :

A char value can be promoted to int type. But char array (char[ ]) can't be promoted to int array (int[ ]) type. int[ ] a ={10 , 20 , 30 , 40}; char[ ] ch = {'a' , 'b' , 'c'}; int[ ] b = a; int[ ] c = ch; // Compile time error : Incompatible types found : char[ ] require : int[ ] Note : Child type array can be assign to the parent type variable. String [ ] s = {"A" , "B" , "C"}; Object[ ] o = s;


Case 2 :

Whenever we are assigning one array to another array, only reference variables will be reassign but not underlying elements. Hence, only types must be matched but not the size.

Example 1 :

int[ ] a ={10 , 20 , 30 , 40 , 50 , 60}; int[ ] b = {70 , 80}; a = b; System.out.println(a[0]); // Output : 70 System.out.println(a[1]); // Output : 80 System.out.println(a[2]); // Runtime Exception : ArrayIndexOutOfBoundsException: 2 // Here, we can assign a to b or vice - versa.

Example 2 :

int[ ][ ] a = new int[3][2]; a[0] = new int[5]; a[1] = new int[4]; a = new int [2][3]; a[0] = new int[2]; System.out.println(a[0][0]); // Output : 0


Case 3 :

Whenever we performing array assignment then dimensions must be matched, i.e. in the place of single dimensional int[ ] array, we should provide only single dimensional int[ ], if by mistake we are providing any other dimension we will get compile time error.

Example :

int[ ][ ] a = new int[3][ ]; a[0] = new int[3]; a[0] = new int[3][2]; // Compile time error : Incompatible types found : int [ ][ ] require : int[ ] a[0] = 10; // Compile time error : Incompatible types found : int require : int[ ]


Jagged Array :

Jagged Array is a multi - dimensional array having fixed number of rows and variable length columns.

Example :

int b[2][ ]; b = new int[2][ ]; b[0] = new int[2]; b[1] = new int[5]; // The above example shows the creation of Jagged Array. // For Jagged Array enhanced for loop is used.


Initialization of Jagged Array :


int a[ ][ ] = {{10 , 20} , {30} , {40 , 50 ,60}}

Example :

class Example { public static void main(String [ ] args) { int a[ ][ ] = {{10 , 20} , {30} , {40 , 50 ,60}}; for(int b[ ] : a) for(int n : b) System.out.println(n); } }

Output :

10 20 30 40 50 60


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.