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?
When you need to store same ‘type’ of data that can be logically grouped together, then you can go for java array.
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
Java Array Instantiation
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
ArrayIndexOutOfBoundsExceptionOne 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 ValuesAfter 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.
Iterating a Java Array
ArrayStoreException – When you try to store a non-compatible value in a java array you get a ArrayStoreException.
Multidimensional ArraysWhen 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.
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 arrayFollowing example source code illustrates on how to assign values and iterate a multidimensional java array.
Sort a Java arrayjava api Arrays contains static methods for sorting. It is a best practice to use them always to sort an array.
Copy a Java arrayYou can use the following options to copy a java array:
- As illustrated above you can use the util calls Arrays. It contains copyOf method for different java types.
- The most used class by a java beginner ‘System‘ has a static method to copy an array.
- Using its clone method you can copy a java array. If the java array is multidimensional, it will be a shallow copy.
- Write your own for loop iterating through the java array and copy elements yourself. (least preferred)