Skip to content

Java Array | Initialization | Length | Methods | Types with Examples

Java Array is an object that contains elements(items) of a similar data type. For example data of Integer or Strings etc. Java Array is a fixed number of Homogeneous (same kind) data with indexing of every element. This is a very useful data type in java.

In this Java array tutorial you will learn about what is Array in Java, some useful methods, how to Initialization, and types with relevant examples.

Java Array overview and Initialization Length Methods Types with Examples

About Arrays:

  1. Fixed in size.
  2. Homogenous elements
  3. The underlying Data structure (DS)

Syntax & Initialization: 

Here is Java Array Initialization syntax.

dataType[] arrName;
//or
dataType []arrName;
//or   
dataType arrName[];

Java Array example

Here is an example of creating and Initialization array values. In this example, we are creating an integer array with a size of 7.

class Hello {
    public static void main(String args[]) {
        int a[] = new int[7];//declaration and instantiation
        a[0] = 7;//initialization
        a[1] = 3;
        a[2] = 9;
        a[3] = 3;
        a[4] = 4;
        a[5] = 8;
        a[6] = 6;
    }
}

Here How it will be indexed and store values.

Java Array Initialization Length Methods Types with Examples

Print Array 

You learned how to create a Java Array, now how do you will print the complete value or specific indexed values? for that See the below code. We are using for loop to traversing an Array and print data. And for Single value fetch you just need to pass the index value. Run the loop for the array we required the size of the array, which can be done by using the length method.

class Hello {
    public static void main(String args[]) {
        int a[] = new int[7];//declaration and instantiation
        a[0] = 7;//initialization
        a[1] = 3;
        a[2] = 9;
        a[3] = 3;
        a[4] = 4;
        a[5] = 8;
        a[6] = 6;
        //traversing array
        for(int i=0;i<a.length;i++)//length is the property of array
            System.out.print(a[i] + " ");

        //print Single item
        System.out.print("\nValue of index 2 is "+ a[2]);
    }
}

Output: 7 3 9 3 4 8 6
Value of index 2 is 9

Java Array Methods

We are looking at only one method, more methods will be explained in another tutorial.

Get Array Length:

Get the Java array length or size you can use in build methods length

class Hello {
    public static void main(String args[]) {
        int a[] = new int[3];
        a[0] = 7;
        a[1] = 3;
        a[2] = 6;

        System.out.print(a.length);
    }
}

Output: 3.

Declare String Array in Java

Here is an example and how to create a string array in java.

class Hello {
    public static void main(String args[]) {
        String a[] = new String[2];
        a[0] = "EyeHunt";
        a[1] = "Tutorial";
    }
}

For print the Array value you can follow the same steps of Int Array in Java example.

The advantage of Array:

  1. In the array, we can represent multiple values with a single variable.
  2. The reusability of the code will be improved.

Java Array types:

There are two types of Array in Java.

  • Single Dimensional Array: Upper examples are all Single Dimensional Array.
  • Multidimensional Array: data is stored in a row and column-based index (also known as matrix form).

Limitations of object type arrays:

  1. Arrays are fixed in size: Once we created an array with some size there is no chance to increase and decrease its size based on our requirement, Hence to use arrays compulsory we should know the size in advance which may or may not possible.
  2. Homogeneous: Arrays can hold only homogeneous data elements.

Example:

Student[] s = new Student; (Correct)

S[0]             = new Customer; (Wrong)

Compiler Exception: Incompatible type

Found: Customer

Required: Student

But we can resolve this problem by using object arrays.

Object[] o = new Object[1000];

o[0]           = new Student[];

o[1]           = new Customer[];

Arrays Concept is not implemented based on some standard data structure hence readymade method support is not available for every requirement we have to write the code explicitly which is the complexity of programming.

Suppose we want to insert an element in some sorting order then who will respond to write the sorting code programmer.

Second, If we want to search whether the particular element is present or not. So in arrays, we will have to write the searching codes.

Array Error : ArrayIndexOutOfBoundsException

If you are inserting or accessing the value of from array and exceeding(greater value) the length of negative then Array Error will throw.

here is an example, our Array size is 3 or length is 4 then trying to get the value in index 5.

class Hello {
    public static void main(String args[]) {
        int a[] = new int[3];
        a[0] = 7;
        a[1] = 3;
        a[2] = 6;

        System.out.print(a[5]);
    }
}

Output Error Screenshot:

Java Array Error ArrayIndexOutOfBoundsException in java array tutorial

Sidenotes: It’s also called One Dimensional Array or 1D Array in java.

Do comment if you have any doubt in this tutorial, and you can give suggestion, examples.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Array Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.

1 thought on “Java Array | Initialization | Length | Methods | Types with Examples”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading