Java arrays
Arrays are a very important concept in Java as well as many other programming languages. An array is a special type of variable which can hold a list of information.
This lesson focuses on:
- The necessity of arrays
- Declaring arrays
- Adding values to an array
- Accessing an arrays elements
- Modifying an arrays elements
- Getting the length of an array
The necessity of arrays
Imagine you are writing a program that has three variables which contain the names of Cars, your code for these variables would probably look like this:
String car1 = "Toyota Camry"; String car2 = "Honda Accord"; String car3 = "Mitsubishi Galant";
With Arrays, it is much simpler. An array gives you the ability to group together related variables into one set. Arrays are special variables which hold lists of information. Therefore, instead of the code above you can declare a 'Cars' array:
String[] Cars = new String[3]; Cars[0] = "Toyota Camry"; Cars[1] = "Honda Accord"; Cars[2] = "Mitsubishi Galant";
Declaring arrays
Arrays are declared with a data type, an array name, and square brackets [] specifying an array.
Syntax:
datatype arrayName[]; OR datatype[] arrayName;
Example:
int evenNumbers[]; which can also be declared as int[] evenNumbers;
The above example declares an array named evenNumbers of data type int. Whether you place the square brackets after the array name or after the data type makes no difference.
After an array is declared, an array object has to be assigned to it using the new keyword as well as the length of the array - specified in the square brackets. The length of the array denotes how many elements an array can hold.
Syntax:
arrayName = new datatype[numElementsInArray];
Example:
evenNumbers = new int[10];
The above example declares an array named evenNumbers of data type int which has a length of 10, therefore it can store 10 elements.
Adding values to an array
You can add values to an array by referring to the appropriate index of the array and assigning it a value.
Syntax:
arrayName[index] = value;
Example:
evenNumbers[3] = 20;
The above example will assign the value 20 to the 4th element in the evenNumbers array.
NOTE: Array indexes begin at 0, so the first element of an array is at index 0, the fifth element of an array is at index 4, and so on.
As an alternative to declaring an array and then assigning values to its elements, you can do both tasks on one line.
Syntax:
dataType[] arrayName = {value, value, value, value, etc.};
Example:
String[] Cars = {"Camry", "Accord", "Galant"};
The above example declares an array named Cars of data type String which stores three elements.
Accessing an arrays elements
You can access an arrays elements by referring to the array by its name and the appropriate index number of the element you wish to access.
Example:
class GetArrayElement{
public static void main(String[] args){
String[] Cars = new String[3];
Cars[0] = "Toyota Camry";
Cars[1] = "Honda Accord";
Cars[2] = "Mitsubishi Galant";
System.out.println("Car: " + Cars[2]);
}
}
Output:
Car: Mitsubishi Galant
In the above example, the third element of the Cars arrays is printed by referring to the Cars array and the index number 2 in brackets.
Modifying an arrays elements
You can modify an arrays elements by referring to the array by its name and the appropriate index number of the element you wish to modify.
Syntax:
arrayName[index] = newValue;
Example:
class ModifyArrayElements{
public static void main(String[] args){
int oddNumbers[] = new int[4];
oddNumbers[0] = 1;
oddNumbers[1] = 3;
oddNumbers[2] = 5;
oddNumbers[3] = 7;
//print the initial value of oddNumbers[0]
System.out.println("oddNumbers[0]: "
+ oddNumbers[0]);
//print the initial value of oddNumbers[2]
System.out.println("oddNumbers[2]: "
+ oddNumbers[2]);
//change the value of oddNumbers[0] to 15
oddNumbers[0] = 15;
//change the value of oddNumbers[2]
// to oddNumbers[3] + 10
oddNumbers[2] = oddNumbers[3] + 10;
//print the new value of oddNumbers[0]
System.out.println("new value of oddNumbers[0]: "
+ oddNumbers[0]);
//print the new value of oddNumbers[2]
System.out.println("new value of oddNumbers[2]: "
+ oddNumbers[2]);
}
}
Output:
oddNumbers[0]: 1
oddNumbers[2]: 5
new value of oddNumbers[0]: 15
new value of oddNumbers[2]: 17
In the above example, the initial value of two elements from the oddNumbers array is printed (oddNumbers[0] and oddNumbers[2]), the value of these elements is changed and the new value of each is then printed.
Getting the length of an array
The length of an array is the number of elements in it. To get the length of an array, use the length property of the Array object with the array whose length you want to find out.
Example:
class GetArrayLength{
public static void main(String[] args){
int oddNumbers[] = new int[4];
oddNumbers[0] = 1;
oddNumbers[1] = 3;
oddNumbers[2] = 5;
oddNumbers[3] = 7;
//print the length of the oddNumbers array
System.out.print("The length of the oddNumbers array
is " + oddNumbers.length);
}
}
Output:
The length of the oddNumbers array is 4
In the above example, the length property is used to return the length of the oddNumbers array.




