Basics of arrays

An array is a linear structure that can store a list of data items. Each of the items stored in the array must have the same type - this is known as the type of the array.

There are two ways to create an array. The first method uses what is known as initializer syntax to create the array and provide a list of values to store in the array.

int[] A = {2,5,1,4}; // Make an array of type int with
// size 4 containing the numbers 2, 5, 1, and 4.

This method is appropriate in cases where you have only a modest number of items to store and you know in advance what those values are. This combination of conditions actually occurs only rarely in practice.

The more common way to construct an array is to use this syntax:

int[] B = new int[10000]; // Make an array to store 10000 ints

To put data in the array we access individual entries in the array via array index notation. Each location in the array has an associated index. The index values are sequential, with 0 as the index of the first item.

You can use the index notation to access locations in the array for either reading or writing:

B[0] = 12; // Put 12 in the first location in the B array
B[1] = 23; // Put 23 in the second location
int x = A[1]; // Copy the value from the second location in A to x

Arrays and loops

Since arrays give us the ability to store a lot of information, we frequently have to automate the process of working with array values. Loops are the natural mechanism to provide that automation.

Here is an example of a for loop used to fill an array with 10000 random doubles.

Random rnd = new Random();
double randomNumbers[] = new double[10000];
for(int n = 0;n < 10000;n++)
  randomNumbers[n] = rnd.nextDouble();

Here is another example that iterates through that list of random numbers to count how many of them are smaller than 0.5:

int count = 0;
for(int n = 0;n < 10000;n++)
  if(randomNumbers[n] < 0.5)
    count++;

All arrays have a length property that you can use to control loops that iterate over the array. Here is the previous example using that length property to control the loop.

int count = 0;
for(int n = 0;n < randomNumbers.length;n++)
  if(randomNumbers[n] < 0.5)
    count++;

Arrays and files

We have already seen that files are a good way to store lots of data for use with loops. Now that we can also use arrays to store lots of data and then manipulate it, the most natural approach is to start by reading the data from a file into an array, and then use the array from that point forward.

Here is some code that reads 100 integers from a file named "numbers.txt" and puts them in an array.

Scanner input = null;
try {
  input = new Scanner(new File("numbers.txt"));
} catch(Exception ex) {
  ex.printStackTrace();
  System.exit(0);
}

int A[] = new int[100];

for(int n = 0;n < 100;n++)
  A[n] = input.nextInt();
input.close();

This code assumes that the file has at least 100 integers available for us to read. In some cases we may not know in advance how many numbers are in the data file. The only reasonable approach in that case is to read the contents of the file twice - once to simply count how many numbers are there, and a second time to actually read the numbers into an array.

Scanner input = null;
try {
  input = new Scanner(new File("numbers.txt"));
} catch(Exception ex) {
  ex.printStackTrace();
  System.exit(0);
}

int N = 0; // Counts how many numbers are in the file.
while(input.hasNextInt()) {
  int unused = input.nextInt();
  N++;
}
input.close();

int A[] = new int[N]; // Make an array with the right size

try {
  input = new Scanner(new File("numbers.txt"));
} catch(Exception ex) {
  ex.printStackTrace();
  System.exit(0);
}

for(int n = 0;n < A.length;n++)
  A[n] = input.nextInt();
input.close();