Points to be noted about Java Array

One thing that i have noted about java arrays is that Arrays can be quite confusing for beginners. If you are not aware of all the aspect of java array, do remember the following points about java array.

You can assign an array a null. Initializing an array without size is not allowed.


You can assign an array a null. Initializing an array without size is not allowed.


int[] array = null; // Works
// Variable must provide either dimension expressions
// or an array initializer
int[] array = new int[]; // Doesn't work

Uninitialized Arrays


Uninitialized Array instance variables are initialized to null. Whereas uninitialized Array instance variables are not initialized. Example:

public class ArrayInitialization {
static int [] arrayInstanceVar;
public static void main(String[] args) {
int [] arrayLocalVar;
System.out.println(arrayInstanceVar);
/* Compiler error: The local variable arrayLocalVar
* may not have been initialized
*/
System.out.println(arrayLocalVar);
}
}

Default initialization of Arrays


When created, arrays are automatically initialized with the default value of their type, this includes even locally declared arrays.

public class ArrayInitialization {
public static void main(String[] args) {
String [] s = new String[10]; // default values: null
Boolean [] b = new boolean[10]; // default values: false
Int [] i = new int[10]; // default values: 0
System.out.println(s[0]);
System.out.println(b[0]);
System.out.println(i[0]);
}
}

Indexing in Arrays


Arrays can be indexed with char (as these can be promoted to int). Whereas using a long index value to access an array causes a compile error.

public class CharIndexedArray {
public static void main(String[] args) {
int [] array = new int[100];
array['A'] = 63;
System.out.println(array[65]); // Prints 63
// Type mismatch: cannot convert from double to int
array[65.0] = 0;
array[65.0L] = 0;
}
}

Array copying example


public class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo)); // caffein
}
}

Array can also be copied form one to another using Arrays.clone() method. Here is an example.

public class ArrayCopyDemo {
public static void main(String[] args) {
String [] source = { "Some", "Useful", "Data"};
// Create one more reference to source array
String [] reference = source;
System.out.println(source == reference);
// Create a copy of the array
String [] destination = source.clone();
for(String s:destination){
System.out.println(s);
}
}
}

Multidimentional Array Declaration


public class MultidimentionalArrayDeclaration {
public static void main(String[] args) {
int [][] array = new int[][2]; // First subscript is must
int [][] array1 = new int[2][]; // Works
int [][] array2 = new int[2][2]; // Works
}
}

Multidimentional Array size


public class MultiDimArrayDemo {
public static void main(String [] args) {
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};
System.out.println("Array length: " + names.length);
System.out.println("Array length: " + names[0].length);
System.out.println("Array length: " + names[1].length);
int a[][] = new int[0][2];
System.out.println("Array length: " + a.length);
}
}

Output:
Array length: 2
Array length: 3
Array length: 2
Array length: 0

Leave a Reply

Your email address will not be published. Required fields are marked *