Arrays of the Built-In Data Types
It is easy to declare and process a handful of variables:
int x1, x2, x3;
x1 = 5;
x2 = 5;
x3 = 5;
Here is a simple visualization of these three variables in memory:

However, it woud be a lot of work to declare and process a large number of variables
. Arrays are a useful way of declaring many variables in one statement. For example, the following line of Java code declares five hundred int variables:
int [] x = new int[500];
For the purposes of this discussion we'll create a slightly smaller array -- the following line of code creates four integer elements:
int [] x = new int[4];
The four elements of the array can be accessed via the names x[0], x[1], x[2] and x[3]. The following four lines of code assign the value five to each of the four elements of the array:
x[0] = 5;
x[1] = 5;
x[2] = 5;
x[3] = 5;
Here is a simple visualization of these four integer variables:
The value between the square braces, [], is refered to as an index or an offset. Any integer expression can be used to specify the index of an element that
you wish to access. The following expressions all refer to the first element of the array, and assign the value five to that element (each of these expressions refer to the array element x[0]):
x[0+0] = 5;
x[2+2-4] = 5;
x[6-(3+3)] = 5;
Variables can also be used in the expressions contained between the square braces. The following instructions also assign the value five to x[0]:
int num = 0;
x[num] = 5;
Usually, a variable is used to keep track of the current position in
an array. This variable is refered to as the index variable. In the following example the expression arr[i] is used to set all five elements of the array arr to the value 30:
int [] arr = new int[5];
int i = 0;
arr[i] = 30;
i++;
arr[i] = 30;
i++;
arr[i] = 30;
i++;
arr[i] = 30;
i++;
arr[i] = 30;
Repeated statements, like those in the example above, are best handled with a loop. The loop shown below accomplishes the same task as the code shown above:
int [] arr = new int[5];
int i = 0;
while (i < 5)
{
arr[i] = 30;
i++;
}
Each array has a member named length that contains the size of the array. The previous example had an array of five ints named arr. The length variable can be accessed via the expression arr.length, and its value is 5. That expression could be used to control the number of times the loop is performed:
int [] arr = new int[5];
int i = 0;
while (i < arr.length)
{
arr[i] = 30;
i++;
}
The following code initializes an array of five elements to the value 33, then
prints out the contents of the array:
public class ArrOne
{
public static void main (String [] args)
{
int [] arr = new int[5];
// This loop assigns the value 33 to every element in the array.
int i = 0;
while (i < 5)
{
arr[i] = 33;
i++;
}
i = 0; // reset i back to the start of array
// This loop prints out the contents of the array.
while (i < arr.length)
{
System.out.println("Index " + i + ": " + arr[i]);
i++;
}
}
}
The above code yields the following output:
Index 0: 33
Index 1: 33
Index 2: 33
Index 3: 33
Index 4: 33
The visualization of an array that was shown earlier in this section was highly simplified. Since an array is an object, the name of the array is a reference to that object. A more accurate visualization of the array created by this line of code
int [] x = new int[4];
would be

Note that this means that you can create several references to the same array object, and refer to the array by any of those names:
int [] x = new int[4];
int [] other;
other = x;
x[0] = 33;
other[1] = 92;

Initializing An Array
A quick way to create an array and to give the array its initial contents is via a declaration and intialization statement as shown below:
int [] tax = { 7, 10, 15, 20, 33 };
The Java compiler counts the number of values inside the curly braces (in this case there are five numbers) and creates an array of that size, then initializes the array to have the indicated values. That one line of code is equivalent to the following seven lines of code:
int [] tax;
tax = new int[5];
tax[0] = 7;
tax[1] = 10;
tax[2] = 15;
tax[3] = 20;
tax[4] = 3;
This kind of initialization can only be done at the time the array is declared.
Exercises - Follow the Code
Show the output of each of the small programs shown below:
-
public class ArrFive
{
public static void main (String [] args)
{
int [] arr = new int[4];
arr[0] = 57;
arr[2] = 35;
arr[1] = 6;
arr[3] = 100;
arr[1] = 5;
int i = 0;
// This loop just prints out the contents of the array
while (i < arr.length)
{
System.out.println("Index " + i + ": " + arr[i]);
i++;
}
}
}
-
public class ArrThree
{
public static void main (String [] args)
{
int [] arr = new int[4];
int i = 0;
arr[i] = 37;
i = 2;
arr[i] = 20;
arr[i - 1] = 54;
arr[i + 1] = 16;
i = 0; // reset i back to the start of array
// This loop just prints out the contents of the array
while (i < arr.length)
{
System.out.println("Index " + i + ": " + arr[i]);
i++;
}
}
}
-
public class ArrSix
{
public static void main (String [] args)
{
int [] arr = new int[5];
int i = 0;
arr[i] = 3;
i = 1;
arr[i] = arr[0] + 22;
arr[2] = arr[i];
arr[3] = i;
arr[4] = arr[i] + i;
i = 0; // reset i back to the start of array
// This loop just prints out the contents of the array
while (i < arr.length)
{
System.out.println("Index " + i + ": " + arr[i]);
i++;
}
}
}
-
public class ArrTwo
{
public static void main (String [] args)
{
int [] arr = new int[5];
int i = 0;
while (i < arr.length)
{
arr[i] = 10 + i;
i++;
}
i = 0; // reset i back to the start of array
// This loop just prints out the contents of the array
while (i < arr.length)
{
System.out.println("Index " + i + ": " + arr[i]);
i++;
}
}
}
-
public class ArrThree
{
public static void main (String [] args)
{
int [] arr = new int[5];
int i = 0;
while (i < arr.length)
{
arr[i] = 3 * i;
i++;
}
i = 0; // reset i back to the start of array
// This loop just prints out the contents of the array
while (i < arr.length)
{
System.out.println("Index " + i + ": " + arr[i]);
i++;
}
}
}
Exercises - Write Your Own Code
For each of the following exercises, copy the given code into jGRASP then add your own code at the indicated location.
-
Add a loop to the code below to print the contents of the val array:
public class ArrayPractice1
{
public static void main(String [] args)
{
int [] val = new int[7];
val[0] = 39;
val[1] = 6;
val[2] = 95;
int i = 3;
while (i < 7)
{
val[i] = i * 10;
i++;
}
// In the space below, write a loop
// to print the contents of val array:
}
}
Your code should generate the following output:
39
6
95
30
40
50
60
- Add a loop to the code below to calculate and print the sum of the val array:
public class ArrayPractice2
{
public static void main(String [] args)
{
int [] val = { 39, 6, 95, 30, 40, 50, 60 };
// In the space below, write a loop
// to calculate and print the sum of the
// values in the val val:
}
}
Your code should generate the following output:
The sum is: 320
- Add a loop to the code shown below to count the number of elements in val that are greater than
or equal to 40. Report the count. Note that the values in the val array are different from those in the array in the preceding problems.
public class ArrayPractice3
{
public static void main(String [] args)
{
int [] val = { 81, 6, 95, 30, 40, 5, 16 };
// In the space below, write a loop
// to count the number of values greater
// than or equal to 40.
}
}
Your program should generate the following output on the console:
There were 3 values greater than 40
-
Add a loop to the code shown below to calculate and print the sum of the odd-valued elements in the array:
public class ArrayPractice4
{
public static void main(String [] args)
{
int [] val = { 81, 6, 95, 30, 40, 5, 16 };
// Report the sum of every odd-valued element
// in the array.
}
}
Your program should generate the following output on the console:
The odd-valued elements had a sum of: 181
-
Add a loop to the code shown below to calculate the sum of the elements that exist at the odd-valued indices of the array (i.e., at indices 1, 3, and 5):
public class ArrayPractice5
{
public static void main(String [] args)
{
int [] val = { 81, 6, 95, 30, 40, 5, 16 };
// Report the sum of the values at the odd
// indices of the array:
}
}
Your program should generate the following output on the console:
The sum of the values at the odd indices: 41
|