Hit enter after type your search item

6 examples of Java array: Create, initialize, and access arrays

The arrays in Java

The array is a type of variable in Java that may hold one or more values of a similar type.

For example, an array of ten elements of a type integer can store 10 numbers.

I will explain the elements and important points about Java arrays, first let me show you a few examples along with code for creating, initializing and accessing arrays.

An example of int array

In this example, an array of five elements is created. The array is declared with type integer so it may store numeric values. A for loop is used to iterate through the array elements. Inside the for loop, the elements are displayed by using System.out.println.

The array Java example code:


Java array

An example of creating, initializing and displaying string array

In this example, a Java string array is created with five elements. The string array contains the names of cities. The array is declared differently than above example. After declaration, the values are assigned to each element as shown in the Java program below:

The string array code:


array string

Syntax of creating arrays

As shown in above two examples, the arrays in Java can be created by different ways.  One way is to use the new keyword as follows:

numArray = new int[5];

In this way, the array is created with five elements. This line of code will allocate the memory of five elements which data type is the integer. Note, the array in Java may contain fixed number of elements. Once created, it cannot be changed.  After creating this array, you may assign values to the array elements as follows:

numArray[0] = 5;

numArray[1] = 10;

numArray[2] = 15;

numArray[3] = 20;

numArray[4] = 25;

There, you may notice the array element index starts at 0. This is important while accessing the array elements as shown in the section below.

Creating array elements with shortcut syntax

You may also create array elements at the time of declaration. For example:

int[] numArray = { 5, 10, 15,

20, 25, 30,


So, you have to specify the data type which is followed by array name on the left side. In the right side, the array element values are given in the curly braces. Each value is separated by a comma.

In that way, the number of elements given in the array becomes the length of that array.

Declaring arrays of other data types

Similarly, you may create arrays by using any of the above ways for different data types. For example:

byte[] byteArray; //It will create an array of byte data type



String[] anArrayOfStrings; //used in one of the above example

How to access Java array elements

The array elements can be accessed by using the numeric index that starts at 0. You may use the array index in square brackets to access elements individually. For example:


See the examples below for accessing single element and using an enhanced for loop for accessing array elements.

An example of accessing array element by index number

Using the same array as used in the second example in above section (World cities array), I will access the element number 4 value by using its index as follows:

The Java code for accessing array element individually:


access array

You have already seen using the for loop for accessing the same array in above section. To learn more about enhanced for loop go to its chapter here.

An example of using multi-dimensional array

The Java also supports multi-dimensional arrays. In this type of array, the array’s components are themselves arrays. The multi-dimensional arrays cantain more than one columns unlike single dimensional arrays used in above section. The rows may vary in length in this type of arrays.

This is how a multidimensional array can be created:

int[ ][ ] NumArray = new int[3][3];

See this example where an array is created and then its elements are displayed by using the for loop.

Java multidimensional array code:


multidimensional array

A program of using array length property

Getting the array length when the number of elements is unknown can be important in different scenarios. The Java provides length property that returns the total number of elements in the specified array. The returned value is an integer.

Have a look at this example where I have used the length property in a for loop to specify the condition for maximum number of times the loop should iterate:

The array code with length property:


array length

You see, by using the first System.out.println, the number of elements in the array is displayed by using this:


After that, the same is used in the for loop for specifying the maximum counter.

A demo of arraycopy method

The arraycopy method of array class can be used for copying the elements from one array to another. The arraycopy method takes a few parameters as described below:


Object copy_from_Array, int sourcePos, Object coptyTo_array, int coptyTo_array_pos, int length

See an example below to see the arraycopy method in action where an array of numbers is created with nine elements. After that, a new array is created where elements will be copied after using the arraycopy method. Finally, the destination array (after copying) elements are displayed:

The code of copying array elements:


array copy

You saw, index 1 to 6 elements from the source array are copied to the destination array.

Summarizing arrays

In Java, the arrays are useful and powerful concept that can be used for storing multiple values of the similar type. I have shown you a few examples after creating and declaring the array including numeric, string, multidimensional arrays. Let me summarize a few important points about Java arrays below:

  • The arrays may contain fixed number of values.
  • The type of the values must be the same or an error will occur.
  • As you declare an array, its length can be specified. Once you have specified the length, it is fixed and cannot be changed.
  • The arrays Java can be created in different ways.
  • You have to specify the type of array e.g. int, byte, short, String etc.
  • The items or elements of an array are accessible by numeric index.
  • The index of the array starts at 0.
  • The array class comes up with a few useful methods and properties to manipulate arrays.
This div height required for enabling the sticky sidebar