Array Basics in Java

     Array Basics in Java


    1. Array Overview

    Array in java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays. 

    • 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

    2. Declaring a Variable to Refer to an Array

    // declares an array of integers
    int[] anArray;
    Ex:
    byte[] anArrayOfBytes;
    short[] anArrayOfShorts;
    long[] anArrayOfLongs;
    float[] anArrayOfFloats;
    double[] anArrayOfDoubles;
    boolean[] anArrayOfBooleans;
    char[] anArrayOfChars;
    String[] anArrayOfStrings;
    Employee[] anArrayOfEmployees;
    Student[] anArrayOfStudent;
    Object[] anArrayOfObjects;
    Can also place the brackets after the array's name:
    // this form is discouraged
    float anArrayOfFloats[];
    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.

    3. Creating an Array

    One way to create an array is with the new operator.
    // create an array of integers
    int[] anArray = new int[10];
    Examples to create an Array:
    String[] anArrayOfStrings = new String[10];
    Object[] anArrayOfObjects = new Object[10];

    4. Initializing Array with Elements

        4.1 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;
        4.2 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";

    5. Accessing an Array

        5.1 Accessing Integer Array Example
    // 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
    
        5.2 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