Java Array
When you need to store same ‘type’ of data that can be logically grouped together, then you can go for java array.
For example, imagine if you had to store the list of countries in individual
java variables and manipulate them. You will have more than hundred variables to manage. Can’t imagine, it would be tedious in any language. Have you ever seen a decent size application programmed without arrays?
In this article you will not find nothing new about java array. Just the same old theory to refresh and I will try to be comprehensive. If you expect some surprise or hidden thing from java, please ALT-F4.
Array is one among the many beautiful things you have in a programming language. Easy to iterate, easy to store and retrieve using their integer index. In java, nobody can deny using an array I challenge. I know you all started with
public static void main(?)
. The argument for the main method is an array.
In java arrays are objects. All methods of an Object can be invoked on an array. Arrays are stored in
heap memory.
Though you view the array as cells of values, internally they are cells of variables. A java array is a group of variables referenced by a common name. Those variables are just references to a
address and will not have a name. These are called the ‘components’ of a java array. All the components must be of same type and that is called as ‘component type’ of that java array.
Java Array Declaration
or
Java Array Instantiation
5 inside the square bracket says that you are going to store five values and is the size of the array ‘n’. When you refer the array values, the index starts from 0 ‘zero’ to ‘n-1′. An array index is always a whole number and it can be a int, short, byte, or char.
Once an array is instantiated, it size cannot be changed. Its size can be accessed by using the length field like .length Its a
java final instance field.
All java arrays implements
Cloneable and
Serializable.
Java array initialization and instantiation together
int marks[] = { 98 , 95 , 91 , 93 , 97 }; |
ArrayIndexOutOfBoundsException
One popular
exception for java beginners is ArrayIndexOutOfBoundsException. You get ArrayIndexOutOfBoundsException when you access an array with an illegal index, that is with a negative number or with a number greater than or equal to its size. This stands second next to
NullPointerException in java for popularity.
Java Array Default Values
After you instantiate an array, default values are automatically assigned to it in the following manner.
- byte – default value is zero
- short – default value is zero
- int – default value is zero
- long – default value is zero, 0L.
- float – default value is zero, 0.0f.
- double – default value is zero, 0.0d.
- char – default value is null, ‘\u0000′.
- boolean – default value is false.
- reference types – default value is null.
Notice in the above list, the
primitives and reference types are treated differently. One popular cause of
NullPointerException is accessing a null from a java array.
Iterating a Java Array
public class IterateJavaArray { |
public static void main(String args[]) { |
int marks[] = { 98 , 95 , 91 , 93 , 97 }; |
System.out.println(value); |
In language C, array of characters is a String but this is not the case in java arrays. But the same behaviour is implemented as a
StringBuffer wherein the contents are mutable.
ArrayStoreException – When you try to store a non-compatible value in a java array you get a ArrayStoreException.
Multidimensional Arrays
When a component type itself is a array type, then it is a multidimensional array. Though you can have multiple dimension nested to n level, the final dimension should be a basic type of
primitive or an Object.
int [] marks, fruits, matrix[]; |
In the above code, matrix is a multidimensional array. You have to be careful in this type of java array declaration.
Internally multidimensional java arrays are treated as arrays of arrays. Rows and columns in multidimensional java array are completely logical and depends on the way you interpret it.
Note: A clone of a java multidimensional array will result in a
shallow copy.
Iterate a java multidimensional array
Following example source code illustrates on how to assign values and iterate a multidimensional java array.
public class IterateMultiDimensionalJavaArray { |
public static void main(String args[]) { |
int sudoku[][] = { { 2 , 1 , 3 }, { 1 , 3 , 2 }, { 3 , 2 , 1 } }; |
for ( int row = 0 ; row < sudoku.length; row++) { |
for ( int col = 0 ; col < sudoku[row].length; col++) { |
int value = sudoku[row][col]; |
Sort a Java array
java api Arrays contains static methods for sorting. It is a best practice to use them always to sort an array.
public static void main(String args[]) { |
int marks[] = { 98 , 95 , 91 , 93 , 97 }; |
System.out.println( "Before sorting: " + Arrays.toString(marks)); |
System.out.println( "After sorting: " + Arrays.toString(marks)); |
No comments:
Post a Comment