JavaDevNotes.com

Java Create Array Examples

Array is the most basic way to represent a group of data with unknown size. Collections has been popular in recent years but arrays will always have a good use case because of it's simplicity. But before we could use an array, we should now how to create an instance. Below are some examples on how we can create array in Java.

Declaration without Creating Instance

Below is an example on how to declare an array in Java without creating an instance:
/**
 * A Simple Example that only declares an array without creating an instance.
 */
public class DeclareJavaArrayNoInstance {
   public static void main(String[] args) {
      int[] myArray;
   }
}
This variable is unusable in our case because it could not contain data. We need to initialize it before we could perform operations on its items.

Create Array Using New

We could create an array in Java using the new operator. This is the simplest way to do it. Below is a simple example on how to illuistrate:
/**
 * A Simple Example that Creates an Array using the new operator
 */
public class SimpleCreateArrayExample {
   public static void main(String[] args) {
      int[] myTestArray = new int[4];
   }
}
The code "new int[4]" creates an instance of array with 4 items. Since we are declaring an array of int, each item in the array contains the default value for an int which is 0. Hence, if we modify the code to be:
/**
 * A Simple Example that Creates an Array using the new operator and prints the values of each item
 */
public class SimpleCreateArrayExample {
   public static void main(String[] args) {
      int[] myTestArray = new int[4];
      System.out.println(myTestArray[0]);
      System.out.println(myTestArray[1]);
      System.out.println(myTestArray[2]);
      System.out.println(myTestArray[3]);
   }
}
The output will be:
0
0
0
0
As we see from the code, arrays in Java are 0-based. It means the index of the first item starts with 0. So the valid list of index for an array with 4 elements are: 0, 1, 2, and 3 and not 1, 2, 3, and 4. If we access values below 0 or beyond the capacity of the array, we will get an exception. For example:
/**
 * A Simple Example that tries to access an item beyond capacity of an array
 */
public class OutOfBoundsExample {
   public static void main(String[] args) {
      int[] myTestArray = new int[4];
      System.out.println(myTestArray[4]);
   }
}

Will throw an exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at OutOfBoundsExample.main(OutOfBoundsExample.java:7)

Because 4 is the index of the 5th element in array, but we only created an array of 4 elements.

Another caveat we need to check is when we declare an array of objects. Below is an example to ullustrate the point

/**
 * A Simple Example that Creates an Array of Objects
 */
public class SimpleCreateArrayOfObjectsExample {
   public static void main(String[] args) {
      Integer[] myTestArray = new Integer[4];
      System.out.println(myTestArray[0]);
      System.out.println(myTestArray[1]);
      System.out.println(myTestArray[2]);
      System.out.println(myTestArray[3]);
   }
}
We replaced int with Integer, which mean the array is now composed by a list of Objects. Now by default, an object has the value null when not initialized. Hence the output of the code above will be:
null
null
null
null
If we want the array to have values, then we could initialize each item. For example:
/**
 * A Simple Example that Creates an Array of Objects and Initialze each item.
 */
public class SimpleCreateArrayOfObjectsWithInitializationExample {
   public static void main(String[] args) {
      Integer[] myTestArray = new Integer[4];
      myTestArray[0] = new Integer(5);
      myTestArray[1] = new Integer(7);
      myTestArray[2] = new Integer(9);
      myTestArray[3] = new Integer(11);
      System.out.println(myTestArray[0]);
      System.out.println(myTestArray[1]);
      System.out.println(myTestArray[2]);
      System.out.println(myTestArray[3]);
   }
}

Will now have the output:

5
7
9
11

Create Array With Initial Values

We could also create an array in Java with initial values. Here is a simple example to show:
/**
 * A Simple Example that Declares And Initialise A Java Array Separately.
 */
public class InitializeJavaArray {
   public static void main(String[] args) {
      int[] myTestArray =  {5, 7, 11};
      System.out.println(myTestArray[0]);
      System.out.println(myTestArray[1]);
      System.out.println(myTestArray[2]);
   }
}
The curly brackets tells Java that we wish to create an array and the values are the ones contained within. In the example above, an array of 3 items is created with values 5, 7, and 11 respectively. hence the output of the code will be:
5
7
11
For more related examples, kindly check this previous post about Initializing Arrays