Array Type in Java

An array is a collection of elements with similar data type.

An array is used to store multiple values of similar type in a single variable.

The square brackets [] are used to declare an array in Java.

An array is declared by defining the array's type and the array's name. An array's type is written as type[], where type is the data type of the elements; the brackets are special symbols indicating that this variable holds an array. The brackets are empty because the size of the array is not a part of its type.


type[] arrayName;

// an array of byte
byte[] byteNumbers;

// an array of short
short[] shortNumbers;

// an array of long
long[] longNumbers;

// an array of float
float[] floatNumbers;

// an array of double
double[] doubleNumbers;

// an array of boolean
boolean[] booleanArrays;

// an array of char
char[] charArrays;

// an array of String
String[] stringArrays;

Create Array and Add Elements

One way to create an array is by using the new keyword and adding elements to the array one by one.


type[] arrayName = new type[arraySize];

Here is an example in which the new keyword is used for creating an array followed by type[arraySize], the arraySize is the number of elements that this array can store. After creating the array, the reference of the newly created array is assigned to the variable on the left. The elements in the array are stored using it's index. The index numbering begins with 0:

String[] fruits = new String[5];
fruits[0] = "Apple";
fruits[1] = "Mango";
fruits[2] = "Grapes";
fruits[3] = "Orange";
fruits[4] = "Papaya";

int[] nums = new int[6];
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
nums[4] = 5;
nums[5] = 6;

Alternatively, an array can also be created and initialized in one line of code.

The elements are declared within a pair of curly braces { } and assign to the array variable on the left. The number of elements provided between braces, and separated by commas, is the length of the array.


String[] fruits = {"Apple", "Mango", "Grapes", "Orange", "Papaya"};

int[] nums = {1, 2, 3, 4, 5, 6};

Access Array Elements

The elements in array are accessed by its numerical index. The index numbering begins with 0. The length of an array is determined while creating an object of array. Once an array is created, the array size cannot be changed.


In this example, the size of the fruits array is 5, which means this array can store five elements.

public class ArrayExample1 {

    public static void main(String[] args) {
        /*Creation of a string array with fixed length of five.*/
        String[] fruits = new String[5];
        /*initialize first element. Since the index numbering 
         * begins with 0.*/
        fruits [0] = "Apple";
        //initialize second element.
        fruits [1] = "Grapes";
        //initialize third element.
        fruits [2] = "Mango";
        //initialize forth element.
        fruits [3] = "Banana";
        //initialize fifth element.
        fruits [4] = "Orange";
        /*An element in array is accessed by its numerical index:*/
        System.out.println("Element 1 at index 0: " + fruits[0]);
        System.out.println("Element 2 at index 1: " + fruits[1]);
        System.out.println("Element 3 at index 2: " + fruits[2]);
        System.out.println("Element 4 at index 3: " + fruits[3]);
        System.out.println("Element 5 at index 4: " + fruits[4]);

Access Array Elements Using Loops

In a real-world programming situation, you would probably use one of these supported looping constructs (for, while, and do-while) to access each element of the array.


public class ArrayExample2 {

    public static void main(String args[]) {
        /*Creation of a string array with fixed size of five*/
        String[] fruits = {"Apple", "Grapes", "Mango", "Orange"};
        /*Accessing elements using for loop*/
        for(int i = 0; i < fruits.length; i++) {
            System.out.println("Element at index " + i + " = "
             + fruits[i]);
Element at index 0 = Apple
Element at index 1 = Grapes
Element at index 2 = Mango
Element at index 3 = Orange

Multidimensional array

In Java, a multidimensional array is an array of arrays. A multidimensional array can be declared by using two or more sets of square brackets, such as String [][] names. The elements are accessed by a corresponding number of index values.


public class DimentionalArrayExample {

    public static void main(String [] args) {

        //Dimentional Array
        String[][] names = { {"Mr. ", "Mrs. ", "Ms. "}, {"A", "B"} };

        //Mr. A
        System.out.println("Name one = "+names [0] [0] + names [1] [0]);

        //Ms. B
        System.out.println("Name two = "+names [0] [2] + names [1] [1]);
Name one = Mr. A
Name two = Ms. B

Find the length of an Array

The built-in length property can be used to determine the size of array.


public class ArrayLengthExample {

   public static void main(String [] args) {
        //5 elements in nums
        int [] nums = {10, 12, 14 , 24, 5};
        System.out.println("The length of nums array is = "
         + nums.length);
The length of nums array is = 5

Copy Elements from Array to Array

The arraycopy method of the Java System class can be used to efficiently copy elements from one array to another:

The arraycopy method of the System class:

public static void arraycopy(Object source, int sourcePosition, 
    Object destination, int destinationPosition, int length)

The source Object argument specify the array to copy from and the destination Object argument specified the array to copy to. The sourcePosition int arguments specify the starting position in the source array, the destinationPosition int argument specify the starting position in the destination array, and the length int argument specify the number of array elements to copy.


public class SystemCopyArrayExample {

    public static void main(String [] args) {
      String [] sourceArray = {"Apple", "Mango", "Grapes", "Banana", "Orange"};
      String [] destinationArray = new String [3];
      //copying elements from one array to another
      System.arraycopy(sourceArray, 1, destinationArray, 0, 3);
[Mango, Grapes, Banana]