Wednesday, December 28, 2011

Initializing Arrays in Java

In Java, arrays can hold two types of data: primitive types (as in byte, short, int, char, etc) and reference types (Object, String, etc).

The main difference between them is that for an array holding elements of a primitive type it is not mandatory to initialize each element. On the other hand, in an object array every element needs to be initialized using the new operator because every element from an object array is be default initialized with null.

A few things about arrays:
  • Also one must note that arrays in Java are zero-based (the first element is at the 0th position)
  • Attempting to access an array with an index less than 0 or greater than the length of the array causes an ArrayIndexOutOfBoundsException to be thrown at runtime.
  • Arrays must be indexed by int values or byte, short or char values (as these can be promoted to int). Using a long index value to access an array may cause a compile error.
  • Arrays are created at runtime so one could easily use a variable to to set the size of the array.
An array can be declared and initialized in the following ways:
//Allocates memory for the array
//Since it's int, all values are by default 0
int[] myInts = new int[3];
//Initializes the array elements
myInts[0] = 1;
myInts[1] = 2;
myInts[2] = 3;
or, if you know the values at compile time and want a shorter form:
int[] myOtherInts = {1,2,3};
Let us suppose you have an simple class like:
public class Person 
   private String name;
   private int age;
   public Person(String name, int age)
   { = name;
       this.age = age;
   public String toString()
    return + " " + this.age;
If you would like to create an array of Person, you could declare it and initialize it as:
//Allocates memory for the array
Person[] myPersons = new Person[3];
//Creates each object
myPersons[0] = new Person("John", 23);
myPersons[1] = new Person("Johnny", 24);
myPersons[2] = new Person("Mike", 25);
or, if you know the values at compile time and want a shorter form:
Person[] myOtherPersons = {
        new Person("John", 23),
        new Person("Johnny", 24),
        new Person("Mike", 25)
It is very important to remember that when you use arrays of objects (reference types) you must allocate memory for each element of the array since every element is initialized by default with null. As you probably may know, if you try to access an object who has a null value, a NullPointerException will thrown. 

An array can also be initialized according to another array or it can be filled automatically with a specific element:
//Copies from source-array myOtherPersons starting from the 0th position 3 
//elements to the destination-array myCopyiedPersons1 starting with the 
//0th position
//Note: myCopyiedPersons1 needs to allocated
Person[] myCopyiedPersons1 = new Person[3];
System.arraycopy(myOtherPersons, 0, myCopyiedPersons1, 0, 3);
//Copies from source-array myOtherPersons the first 3 elements to the 
//destination-array myCopyiedPersons2 
//Note: myCopyiedPersons2 doesn't need to be allocated.
Person[] myCopyiedPersons2 = null;
myCopyiedPersons2 = Arrays.copyOf(myOtherPersons, 3);
//Copies from source-array myOtherPersons the elements between the indexes 1 
//and 3 (not including 3) to destination-array myCopyiedPersons3. 
//In myCopyiedPersons3 the element at the 0th position will be equivalent 
//to the element at the 1th (starting index) from myOtherPersons.
//Note: myCopyiedPersons3 doesn't need to be allocated.
Person[] myCopyiedPersons3 = null;
myCopyiedPersons3 = Arrays.copyOfRange(myOtherPersons, 1, 3);
//Makes all elements of the array myCopyiedPersons4 between the indexes 0 
//and 3 (not including 3) to be equal with a certain specified object
//Note: myCopyiedPersons4 needs to allocated
Person[] myCopyiedPersons4 = new Person[3];
Arrays.fill(myCopyiedPersons4, 0, 3, new Person("John",23));

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger