Using Arrays in Java

This article tells users the fundamental uses of Java Arrays. Java, or any other programming language, has similar definitions on how to use arrays. An array is a grouping of the same types of variables into an indexable structure. A one-dimensional array is defined in Java in the form of:

String[] strings = new String[SIZE];

The first “String[]” tells the compiler that a String array is being declared. The “string” is the variable reference for the rest of the program. The “new” allocates memory for the String array. The String[SIZE] tells the compiler to allocated SIZE times the space needed to store addresses to Strings in memory for this array.

When computers were first used, memory was very expensive. It was a bad idea to waste any memory to make programming easier to understand. Thus, the pointer to the first element of an array is the same as the pointer to the array itself. The offset or index of an element of an array is based on the offset from the base of array to its location. Since the first element is at the same address as the array, its offset is zero (0). A simple diagram may be helpful.

Array Address vs First Element Address
Addressing the First Member of the Array

Parallel Arrays

Often arrays are defined one-dimensional (single-dimensional). To have related information easily accessible, parallel arrays were often used. For example, the fifth element of one array was related to the fifth element of a second array in the same way as the second element of the first array is related to the second element of the second array. If the relationship is a brand of a car in the first array to a model of vehicle in the second array an example would be Ford and F-150 in the two arrays at the same index. Another example would be Toyota and Rav 4 at the same index in the two arrays. A Chart might be helpful:

String[] brands = new String[SIZE];
String[] models = new String[SIZE];

To function properly, the two arrays must be of the same size.

Parallel Arrays
Parallel Arrays

Two-Dimensional Array

It may be easier to combine the information into a single two-dimensional array. In the above example, each row would contain the make and model similar to the parallel array in the previous example. So, the first row could contain Toyota and Rav 4 while the second row could contain Ford and F-150. There is no practical limit to the number of rows and columns in a two-dimensional array. An example of a two-dimensional array with five rows and 4 columns follows. The row can be considered addresses to the content of each column. The definition is:

String[][] vehicles = new String[ROWS][COLUMNS];

Where ROWS indicates the number of rows used (in the following example, 5) and COPLUMNS indicates the number of columns used (in the following example, 4).

Two-Dimensional Array
Two-Dimensional Array

Summary

Arrays are very helpful. They store information that is interchangeable so that the same code will handle two different elements in the same one-dimensional array or in the same column of a two-dimensional array. I leave with the following video.

This entry was posted in Computers, Java, Software and tagged , . Bookmark the permalink.