Arrays

In Java, an array is used for storing elements of the same data type in a fixed-sized collection, ordered by insertion. Being fixed in size means that unlike with ArrayList, elements cannot be removed or added after the array has been created. They can only be modified.

Syntax

Array variables are defined with a type followed by square brackets []. Next, the actual elements are comma-separated and surrounded by curly brackets {}:

dataType[] varName = {elemOne, elemTwo, elemThree, elemN};

Alternatively, an array can be defined with the new keyword, the arrayLength integer defined, and each of the elements defined afterward:

dataType[] varName = dataType[arrayLength];

varName[0] = valueOne;
varName[1] = valueTwo;
varName[n] = valueN;

It is important the referenced index is valid. Otherwise, an ArrayIndexOutOfBoundsException will be thrown.

Example

In the example below, an array can be created in the following ways:

int[] semesterOneMarks = {10, 20, 30, 40, 50};
String[] semesterOneCourses = new String[5];
semesterOneCourses[0] = "Algebra";
semesterOneCourses[1] = "World History";
semesterOneCourses[2] = "Literature";
semesterOneCourses[3] = "Programming";
semesterOneCourses[4] = "Gym";

Elements within the array can be accessed and changed as well:

System.out.print(semesterOneCourses[0] + ": ");
System.out.println(semesterOneMarks[0]);
// Changed from "World History" to "History"
semesterOneCourses[1] = "History";
System.out.print(semesterOneCourses[1] + ": ");
System.out.println(semesterOneMarks[1]);
System.out.print(semesterOneCourses[2] + ": ");
System.out.println(semesterOneMarks[2]);
System.out.print(semesterOneCourses[3] + ": ");
System.out.println(semesterOneMarks[3]);
System.out.print(semesterOneCourses[4] + ": ");
System.out.println(semesterOneMarks[4]);

The output will look like this:

Algebra: 10
History: 20
Literature: 30
Programming: 40
Gym: 50

The Arrays Class

Java includes an Arrays class in java.util that provides a number of static methods for manipulating arrays. Prominent ones are listed below.

Arrays

.fill()
Fills an array with a specified value.
.sort()
Sorts an array into ascending order.

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy