Java Array is a container that can hold a fixed number of values of the same type. The values can be of the primitive type like int, short, byte, or it can be an object like String, Integer etc.

Java Array

  • Despite the fact that array can hold primitive type and object, array itself is an object in java heap, even if it is declared to hold primitive type data.
  • We need to specify the type of values at the time of declaring the array.
  • Array size needs to be provided at the time of initialization.
  • Java Arrays is the utility class that provides a lot of useful methods to work with arrays.
  • We can access array elements using integer index.
  • We can traverse through array elements using java for loop or java forEach loop.
  • Array can be one dimensional as well as multidimensional.
  • ArrayIndexOutOfBoundsException exception is raised when we try to access array element by specifying index value larger than the size of array.
  • List implementations such as ArrayList is backed by array. There are few utility methods to convert list to array and vice versa.
  • There are utility methods to search for an element in the array or to sort an array. We will look into these through example programs in later section of this tutorial.
  • java-array (1)

Let’s have a look at some important points about array through programs.

Array Declaration in Java

An Array can be declared by stating the type of data that array will hold (primitive or object) followed by the square bracket and variable name.
An array can be one dimensional or it can be multidimensional. Multidimensional arrays are in fact arrays of arrays.

  1. Declaring an array of primitive type of data (one dimensional):
  2. Declaring an array of object type (one dimensional):
  3. Declaring multidimensional array:

When we are declaring an array it’s recommended that we put square bracket immediately after the declared type.

Java initialize array

We can initialize an array using new keyword or using shortcut syntax, which creates and initialize array at the same time.

Creating an array using new keyword means we are creating an array object in java heap and to create an object java needs to know how much space to allocate on the heap for that object. So we need to specify the size of the array at the time of initialization.

java-initialize-array

  1. Initializing one dimensional array:
  2. Initializing multidimensional array:
  3. Initializing an array using shortcut syntax:

    If you notice above, the two dimensional array intArr2 is not a symmetric matrix. It’s because a multidimensional array in java is actually an array of array. For complete explanation, refer Two Dimensional Array in Java.
  4. Invalid ways to initialize an array: Here are some invalid ways to initialize an array.
  5. Non recommended way to initialize an array: Here are some other variations of initializing arrays in java but they are strongly discouraged to avoid confusion.

Accessing Array Elements

  • Array elements can be accessed by its index and it always start with the 0 (zero).
  • Array object have public variable called length, which gives the number of elements in the array.
  • We can process or traverse array elements using for or foreach loop.

Let’s have look at the below example program for accessing array elements in java.

Now let’s look at an example of printing two-dimensional array using nested for loops.

What is ArrayIndexOutOfBoundsException?

Sometimes we try to access element outside the array size; for example if we have an array of size 10 and if we try to access 11th element then compiler will throw ArrayIndexOutOfBoundsException.

Let’s have a look at the below example program.

Output of above program is:

Converting Array to List and Vice Versa

Output of above program is:

Java Sort Array

Arrays can be sorted by using java.util.Arrays sort method which sorts the given array into an ascending order. Below is a simple program for sorting arrays.

Output of above program is:

Searching for element in Java Array

java.util.Arrays provides binarySearch method which uses binary search algorithm to search specified value from the given array. Below is a simple program for searching element in an array using binary search. The array must be sorted prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

Output of above program is:

Java Copy Array

Object class provides clone() method and since an array in java is also an Object, you can use this method to achieve full array copy. This method will not suit you if you want a partial copy of the array. Below is a simple program for copying arrays in java.

There are many other ways to copy the array in java. Please refer java copy array to learn other ways.

Add elements to Array

There is no shortcut method to add elements to an array in java. But as a programmer, we can write one. Here I am providing a utility method that we can use to add elements to an array.

Output of the above program is:

Remove element from Array

Below is a simple program to remove element from array.

Output of the above program is:

That’s all for Java Array tutorial, I hope nothing important got missed here.

Reference: Oracle Documentation

By admin

Leave a Reply

%d bloggers like this: