Java Array Basics Guide

Array Overview

  • An array is a container object that holds a fixed number of values of a single type.
  • The length of an array is established when the array is created. After creation, its length is fixed.
  • As we know Array is a data structure where we store similar elements and Array a starts from index 0.
  • Each item in an array is called an element, and each element is accessed by its numerical index.
  • Since arrays are objects in Java, we can find their length using member length.
  • A Java array variable can also be declared like other variables with [] after the data type.
  • The variables in the array are ordered and each has an index beginning from 0.
  • Java array can be also be used as a static field, a local variable or a method parameter.
  • The size of an array must be specified by an int value and not long or short.

Advantage of Java Array

  • Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
  • Random access: We can get any data located at any index position.

Disadvantage of Java Array

  • Size Limit: We can store the only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.

Types of Array in java

There are two types of array.
  1. Single Dimensional Array
  2. Multidimensional Array

Array Simple Example

The following program, ArrayDemo, creates an array of integers, puts some values in the array, and prints each value to standard output.
public class ArrayBasics {
        public static void main(String[] args) {
                // declares an array of integers
        int[] anArray;
 
        // allocates memory for 10 integers
        anArray = new int[10];
            
        // initialize first element
        anArray[0] = 100;
        // initialize second element
        anArray[1] = 200;
        // and so forth
        anArray[2] = 300;
        anArray[3] = 400;
        anArray[4] = 500;
        anArray[5] = 600;
        anArray[6] = 700;
        anArray[7] = 800;
        anArray[8] = 900;
        anArray[9] = 1000;
 
        for (int i = 0; i < anArray.length; i++) {
                System.out.println("Element at index " + i + ": " +anArray[i]);
                }
        }
}
Output:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000

Declaring a Variable to Refer to an Array

The preceding program declares an array (named anArray) with the following line of code:
// declares an array of integers
int[] anArray;
An array declaration has two components: the array's type and the array's name.
  1. An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty).
  2. A variable like from above program anArray is variable, the declaration does not actually create an array; it simply tells the compiler that this variable will hold an array of the specified type.
Few more examples for declaring an Array.
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
Employee[] anArrayOfEmployees;
Student[] anArrayOfStudent;
Object[] anArrayOfObjects;
You can also place the brackets after the array's name:
// this form is discouraged
float anArrayOfFloats[];
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.

Creating an Array

One way to create an array is with the new operator.
// create an array of integers
int[] anArray = new int[10];
Above line of code allocates an array with enough memory for 10 integer elements and assigns the array to the anArray variable.
Few more examples to create an Array:
String[] anArrayOfStrings = new String[10];
Object[] anArrayOfObjects = new Object[10];

Initializing Array with Elements

Initialize Integer Array Example

Let's create and initialize integer Array with few integer elements
// initialize primitive one dimensional array
int[] anArray = new int[5];

anArray[0] = 10; // initialize first element
anArray[1] = 20; // initialize second element
anArray[2] = 30; // and so forth
anArray[3] = 40;
anArray[4] = 50;

Initialize String Array Example

Let's create and initialize String Array with few String elements.
// initialize Object one dimensional array
String[] anArrayOfStrings = new String[5];
anArrayOfStrings[0] = "abc"; // initialize first element
anArrayOfStrings[1] = "xyz"; // initialize second element
anArrayOfStrings[2] = "name"; // and so forth
anArrayOfStrings[3] = "address";
anArrayOfStrings[4] = "id";

Initialize Employee Array Example

Let's create and initialize Employee Array with few employee objects.
Employee[] anArrayOfEmployees = new Employee[2];
anArrayOfEmployees[0] = new Employee(10, "ramesh");
anArrayOfEmployees[1] = new Employee(11, "john");

class Employee {
        private int id;
        private String name;

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public Employee(int id, String name) {
                super();
                this.id = id;
                this.name = name;
        }
}

Accessing an Array

Accessing Integer Array Example

Let's create integer Array, initialize with few elements and access integer Array with indexing.
// initialize primitive one dimensional array
int[] anArray = new int[5];

anArray[0] = 10; // initialize first element
anArray[1] = 20; // initialize second element
anArray[2] = 30; // and so forth
anArray[3] = 40;
anArray[4] = 50;

// Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);
System.out.println("Element 4 at index 3: " + anArray[3]);
System.out.println("Element 5 at index 4: " + anArray[4]);
Output:
Element 1 at index 0: 10
Element 2 at index 1: 20
Element 3 at index 2: 30
Element 4 at index 3: 40
Element 5 at index 4: 50

Accessing String Array Example

Let's create String Array, initialize with few elements and access String Array with indexing.
// initialize Object one dimensional array
String[] anArrayOfStrings = new String[5];
anArrayOfStrings[0] = "abc"; // initialize first element
anArrayOfStrings[1] = "xyz"; // initialize second element
anArrayOfStrings[2] = "name"; // and so forth
anArrayOfStrings[3] = "address";
anArrayOfStrings[4] = "id";

// Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + anArrayOfStrings[0]);
System.out.println("Element 2 at index 1: " + anArrayOfStrings[1]);
System.out.println("Element 3 at index 2: " + anArrayOfStrings[2]);
System.out.println("Element 4 at index 3: " + anArrayOfStrings[3]);
System.out.println("Element 5 at index 4: " + anArrayOfStrings[4]);
Output:
Element 1 at index 0: abc
Element 2 at index 1: xyz
Element 3 at index 2: name
Element 4 at index 3: address
Element 5 at index 4: id

Accessing Employee Array Example

Let's create Employee Array, initialize with few elements and access employee Array with indexing.
Employee[] anArrayOfEmployees = new Employee[2];
anArrayOfEmployees[0] = new Employee(10, "ramesh");
anArrayOfEmployees[1] = new Employee(11, "john");

// Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + anArrayOfEmployees[0].getName());
System.out.println("Element 2 at index 1: " + anArrayOfEmployees[1].getName());

class Employee {
        private int id;
        private String name;

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public Employee(int id, String name) {
                super();
                this.id = id;
                this.name = name;
        }
}
Output:
Element 1 at index 0: ramesh
Element 2 at index 1: john

Shortcut Syntax to Create and Initialize an Array

Alternatively, we can use the shortcut syntax to create and initialize an array:
Shortcut syntax to create and initialize an integer array example:
// shortcut syntax to create and initialize an array
int[] array = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
// Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + array[0]);
System.out.println("Element 2 at index 1: " + array[1]);
System.out.println("Element 3 at index 2: " + array[2]);
System.out.println("Element 4 at index 3: " + array[3]);
System.out.println("Element 5 at index 4: " + array[4]);
Output:
Element 1 at index 0: 100
Element 2 at index 1: 200
Element 3 at index 2: 300
Element 4 at index 3: 400
Element 5 at index 4: 500
Shortcut syntax to create and initialize a string array example:
// shortcut syntax to create and initialize an array
String[] arrayOfStrings = {"abc", "xyz", "name", "address", "id"};
// Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + arrayOfStrings[0]);
System.out.println("Element 2 at index 1: " + arrayOfStrings[1]);
System.out.println("Element 3 at index 2: " + arrayOfStrings[2]);
System.out.println("Element 4 at index 3: " + arrayOfStrings[3]);
System.out.println("Element 5 at index 4: " + arrayOfStrings[4]);
Output:
Element 1 at index 0: abc
Element 2 at index 1: xyz
Element 3 at index 2: name
Element 4 at index 3: address
Element 5 at index 4: id
Shortcut syntax to create and initialize an employee array example:
// shortcut syntax to create and initialize an array
Employee[] employees = {new Employee(10, "ramesh"),new Employee(11, "john")};
// Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + employees[0].getName());
System.out.println("Element 2 at index 1: " + employees[1].getName());
Output:
Element 1 at index 0: ramesh
Element 2 at index 1: john

Complete Array Basics Source Code Example

/**
 * Class demonstrate the basics of Array
 * @author javaguides.net
 *
 */
public class JavaArrayDemonstration {

        public static void main(String[] args) {
                // initialize primitive one dimensional array
                int[] anArray = new int[5];

                anArray[0] = 10; // initialize first element
                anArray[1] = 20; // initialize second element
                anArray[2] = 30; // and so forth
                anArray[3] = 40;
                anArray[4] = 50;

                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + anArray[0]);
                System.out.println("Element 2 at index 1: " + anArray[1]);
                System.out.println("Element 3 at index 2: " + anArray[2]);
                System.out.println("Element 4 at index 3: " + anArray[3]);
                System.out.println("Element 5 at index 4: " + anArray[4]);

                // initialize Object one dimensional array
                String[] anArrayOfStrings = new String[5];
                anArrayOfStrings[0] = "abc"; // initialize first element
                anArrayOfStrings[1] = "xyz"; // initialize second element
                anArrayOfStrings[2] = "name"; // and so forth
                anArrayOfStrings[3] = "address";
                anArrayOfStrings[4] = "id";

                
                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + anArrayOfStrings[0]);
                System.out.println("Element 2 at index 1: " + anArrayOfStrings[1]);
                System.out.println("Element 3 at index 2: " + anArrayOfStrings[2]);
                System.out.println("Element 4 at index 3: " + anArrayOfStrings[3]);
                System.out.println("Element 5 at index 4: " + anArrayOfStrings[4]);

                Employee[] anArrayOfEmployees = new Employee[2];
                anArrayOfEmployees[0] = new Employee(10, "ramesh");
                anArrayOfEmployees[1] = new Employee(11, "john");

                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + anArrayOfEmployees[0].getName());
                System.out.println("Element 2 at index 1: " + anArrayOfEmployees[1].getName());

                
                // shortcut syntax to create and initialize an array
                int[] array = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + array[0]);
                System.out.println("Element 2 at index 1: " + array[1]);
                System.out.println("Element 3 at index 2: " + array[2]);
                System.out.println("Element 4 at index 3: " + array[3]);
                System.out.println("Element 5 at index 4: " + array[4]);
                
                // shortcut syntax to create and initialize an array
                String[] arrayOfStrings = {"abc", "xyz", "name", "address", "id"};
                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + arrayOfStrings[0]);
                System.out.println("Element 2 at index 1: " + arrayOfStrings[1]);
                System.out.println("Element 3 at index 2: " + arrayOfStrings[2]);
                System.out.println("Element 4 at index 3: " + arrayOfStrings[3]);
                System.out.println("Element 5 at index 4: " + arrayOfStrings[4]);
                
                
                // shortcut syntax to create and initialize an array
                Employee[] employees = {new Employee(10, "ramesh"),new Employee(11, "john")};
                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + employees[0].getName());
                System.out.println("Element 2 at index 1: " + employees[1].getName());
                
        }
}

class Employee {
        private int id;
        private String name;

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public Employee(int id, String name) {
                super();
                this.id = id;
                this.name = name;
        }
}
Output:
Element 1 at index 0: 10
Element 2 at index 1: 20
Element 3 at index 2: 30
Element 4 at index 3: 40
Element 5 at index 4: 50
Element 1 at index 0: abc
Element 2 at index 1: xyz
Element 3 at index 2: name
Element 4 at index 3: address
Element 5 at index 4: id
Element 1 at index 0: ramesh
Element 2 at index 1: john
Element 1 at index 0: 100
Element 2 at index 1: 200
Element 3 at index 2: 300
Element 4 at index 3: 400
Element 5 at index 4: 500
Element 1 at index 0: abc
Element 2 at index 1: xyz
Element 3 at index 2: name
Element 4 at index 3: address
Element 5 at index 4: id
Element 1 at index 0: ramesh
Element 2 at index 1: john

Related Array Posts

Comments