Working With Arrays in Java

Young Developer Working In His Office.
vgajic/Getty Images

If a program needs to work with a number of values of the same data type, you could declare a variable for each number. For example, a program that displays lottery numbers:

int lotteryNumber1 = 16;
int lotteryNumber2 = 32;
int lotteryNumber3 = 12;
int lotteryNumber4 = 23;
int lotteryNumber5 = 33;
int lotteryNumber6 = 20;

A more elegant way of dealing with values which can be grouped together is to use an array.

An array is a container that holds a fixed number of values of a data type. In the above example, the lottery numbers could be grouped together in an int array:

int[] lotteryNumbers = {16,32,12,23,33,20};

Think of an array as a row of boxes. The number of boxes in the array cannot change. Each box can hold a value as along as it is of the same data type as the values contained within the other boxes. You can look inside a box to see what value it contains or replace the contents of the box with another value. When talking about arrays, the boxes are called elements.

Declaring and Initializing an Array

The declaration statement for an array is similar to the one used to declare any other variable. It contains the data type followed by the name of the array - the only difference is the inclusion of square brackets next to the data type:

int[] intArray;
float[] floatArray; 
char[] charArray;

The declaration statements above tell the compiler that intArrayvariable is a array of ints, floatArrayis an array of floatsand charArrayis an array of chars.

Like any variable, they cannot be used until it has been initialized by assigning it a value. For an array the assignment of a value to an array must define the size of an array:

intArray = new int[10];

The number inside the brackets defines how many elements the array holds.The above assignment statement creates an int array with ten elements.

Of course, there's no reason why the declaration and assignment can't happen in one statement:

float[] floatArray = new float[10];

Arrays are not limited to primitive data types. Arrays of objects can be created:

String[] names = new String[5];

Using an Array

Once an array has been initialized the elements can have values assigned to them by using the array's index. The index defines the position of each element in the array. The first element is at 0, the second element at 1 and so on. It's important to note that the index of the first element is 0. It's easy to think that because an array has ten elements that the index is from 1 to 10 instead of from 0 to 9. For example, if we go back to the lottery numbers example we can create an array containing 6 elements and assign the lottery numbers to the elements:

int[] lotteryNumbers = new int[6];
lotteryNumbers[0] = 16;
lotteryNumbers[1] = 32;
lotteryNumbers[2] = 12;
lotteryNumbers[3] = 23;
lotteryNumbers[4] = 33;
lotteryNumbers[5] = 20;

There is a shortcut to filling elements in an array by putting the values for the elements in the declaration statement:

int[] lotteryNumbers = {16,32,12,23,33,20};

String[] names = {"John", "James", "Julian", "Jack", "Jonathon"};

The values for each element is placed inside a pair of curly brackets. The order of the values determines which element is assigned the value starting with index position 0. The number of elements in the array is determined by the number of values inside the curly brackets.

To get the value of an element its index is used:

System.out.println("The value of the first element is " + lotteryNumbers[0]); 

To find out how many elements an array has use the length field:

System.out.println("The lotteryNumbers array has " + lotteryNumbers.length + " elements");

Note: A common mistake when using the length method is to forget is to use the length value as an index position. This will always result in an error as the index positions of an array are 0 to length - 1.

Multidimensional Arrays

The arrays we have been looking at so far are known as one-dimensional (or single dimensional) arrays.

This means they only have one row of elements. However, arrays can have more than one dimension. A multidimensional is actually an array that contains arrays:

int[][] lotteryNumbers = {{16,32,12,23,33,20},{34,40,3,11,33,24}};

The index for a multidimensional array consists of two numbers:

System.out.println("The value of element 1,4 is " + lotteryNumbers[1][4]);

Although the length of the arrays contained within a multidimensional array do not have to be the same length:

String[][] names = new String[5][7];

Copying an Array

To copy an array the easiest way is to use the arraycopymethod of the System class. The arraycopymethod can be used to copy all the elements of an array or a subsection of them. There are five parameters passed to the arraycopymethod - the original array, the index position to start copying​ an element from, the new array, the index position to start inserting from, the number of elements to copy:

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

For example, to create a new array containing the last four elements of an int array:

int[] lotteryNumbers = {16,32,12,23,33,20};
int[] newArrayNumbers = new int[4];
System.arraycopy(lotteryNumbers, 2, newArrayNumbers, 0, 4);

As arrays are a fixed length the arraycopymethod can be a useful way to change the size of an array.

To further your knowledge about arrays you can learn about manipulating arrays using the Arrays class and making dynamic arrays (i.e., arrays when the number of elements is not a fixed number) using the ArrayList class.

Format
mla apa chicago
Your Citation
Leahy, Paul. "Working With Arrays in Java." ThoughtCo, Mar. 29, 2017, thoughtco.com/working-with-arrays-2034318. Leahy, Paul. (2017, March 29). Working With Arrays in Java. Retrieved from https://www.thoughtco.com/working-with-arrays-2034318 Leahy, Paul. "Working With Arrays in Java." ThoughtCo. https://www.thoughtco.com/working-with-arrays-2034318 (accessed October 20, 2017).