This article is going to sum up the important points about arrays in the Java language, with code examples.

In general, array is a built-in data structure that holds a set of elements of the same type. Arrays are useful and indispensable in programming.

Here are the characteristics of arrays in Java:

 

1. Java Arrays declarations and initialization

Declare first and initialize later:

int [] numbers = new int[10];

This declares an array object to hold 10 integer numbers (primitive array).

When declaring an array of primitive type, remember these rules:

Then we initialize values for each element of the array like this:

numbers[0] = 10;
numbers[1] = 500;
numbers[2] = 1000;
...

The following statement declares an array of String objects:

String[] names = new String[5];

This array holds 5 String objects. And by default, all elements of Object type are initialized to null.

NOTE: In Java, you can place the brackets [] either after the type or after the variable name. Hence these declarations are both correct:

String[] names = new String[5];
String titles[] = new String[10];

However, it’s recommended to use the [] after the type for readability: You can easily realize this is an array of Strings, or that is an array of integer numbers.

You can also declare and initialize elements of an array in one statement. For example:

int[]  numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

This creates an array with 10 integer numbers which are initialized up declaration. This is a handy shortcut for declaring arrays whose elements are already known at compile time.

This statement declares and initializes an array of Strings:

String[] columnNames = {“No”, “Name”, “Email”, “Address”};

 

2. Java Multi-dimensional arrays

We can create an array of arrays (2-dimension array). For instance:

float[][] matrix = new float[5][3];

This creates a 2D array with is equivalent to a matrix of 5 rows and 3 columns.

Another example:

String[][] sampleData = {
		{"a", "b", "c", "d"},
		{"e", "f", "g", "h"},					
		{"i", "j", "k", "l"},
		{"m", "n", "o", "p"},				
	};

This creates a 4x4 array and initializes all the elements.

We can also create a 3D array, 4D array, etc using the same technique. 

3. Arrays Manipulation in Java

We access elements in the array by index (remember 0-based):

String firstColumn = columnNames[0];

This statement takes value of the first element in the String array columnNamesand assigns it to the variable firstColumn.

The following statement illustrates accessing an element in a 2D array:

String[] firstRow = sampleData[0];

This gets the first element in the sampleData array, which returns an array.

The following statement takes the element at 3rd row and 2nd column in the above 2D array:

String letter = sampleData[4][3];

And these examples show how to assign values to elements in arrays:

columnNames[2] = “Phone;
columnNames[4] = new String(“City”);
numbers[4] = 1024;
sampleData[4][2] = “xyz”;

A common operation is iterating an array using a loop statement like the for statement. The following example uses the for loop to iterate over all elements in an array of integer numbers, and prints value of each element:

int[]  numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for (int i = 0; i < numbers.length; i++) {
	System.out.println(numbers[i]);
}

 

For arrays of objects you can use the for eachsyntax to iterate. For example:

String[] nameList = {"Tom", "Mary", "Peter", "John", "Adam", "Justin"};

for (String aName : nameList) {
	System.out.println(aName);
}

 

Besides the fundamental operations like getting and setting, Java provides various functions to manipulate arrays in the Arrays class.

The Arrays is a utility class which can be found in the java.util package. Here are some noteworthy methods it provides:

In addition, the System.arraycopy() is an efficient method for copying elements from one array to another. Remember using this method instead of writing your own procedure because this method is very efficient.

So far I have walked you through a tour of arrays in Java. Here’s the summary for today:

API References:

 

Related Array Tutorials:

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.