Single Array
- Sample Problem: Project Arrays, Class SingleArray
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
(An array of ten elements)
Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.
The following program, ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output.
class ArrayDemo
{
public static void main(String[] args)
{
int[] anArray; // declares an array of integers
anArray = new int[10]; // allocates memory for 10 integers
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Element at index 4: " + anArray[4]);
System.out.println("Element at index 5: " + anArray[5]);
System.out.println("Element at index 6: " + anArray[6]);
System.out.println("Element at index 7: " + anArray[7]);
System.out.println("Element at index 8: " + anArray[8]);
System.out.println("Element at index 9: " + anArray[9]);
}
}
The output from this program is:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
In a real-world programming situation, you'd probably use one of the supported looping constructs to iterate through each element of the array, rather than write each line individually as shown above. However, this example clearly illustrates the array syntax.
Declaring a Variable to Refer to an Array
The above program declares an array variable named anArray with the following line of code:
int[] anArray; // declares an array of integers
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions as previously discussed in the variables section. As with variables of other types, the declaration does not actually create an array ? it simply tells the compiler that this variable will hold an array of the specified type.
Similarly, you can declare arrays of other types:
an array to hold number with decimal values would be declared as: double[] anArrayOfDoubles;
an array to hold either true or false would be declared as: boolean[] anArrayOfBooleans;
an array to hold single keyboard characters would be declared as: char[] anArrayOfChars;
an array to hold multiple keyboard characters would be declared as: String[] anArrayOfStrings;
Creating, Initializing, and Accessing an Array
One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for ten integer elements and assigns the array to the anArray variable.
anArray = new int[10]; // create an array of integers
If this statement were missing, the compiler would print an error like the following, and compilation would fail:
ArrayDemo.java:4: Variable anArray may not have been initialized.
The next few lines assign values to each element of the array:
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);
Alternatively, you can use the shortcut syntax to create and initialize an array:
int[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
Here the length of the array is determined by the number of values provided between { and }.
You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.
For each of the Programming 1 lessons you are strongly encouraged to view and play around with the sample code provided in the BlueJ Java Student Samples folder.
Open BlueJ and choose Open Project from the Project menu. Navigate to your Programming 1 folder on the I: drive and open the BlueJ Java Student Samples folder. Open the project named Arrays.
Lesson Example: Project Arrays, Class SingleArray, methods simpleArray() and StringArray()
- Right-click on the Class named SingleArray and select new SingleArray()
- Right-click on the red rectangle and choose the method named simpleArray()
- Right-click on the red rectangle and choose the method named StringArray()
Code for method simpleArray():
void simpleArray()
{
int num[] = new int[5]; //creates an integer array object with indexes from 0 to 4
int total =0;
num[0] = 5;
num[1] = 45;
num[2] = 82;
num[3] = 33;
num[4] = 1;
total = num[4] + num[3-2] + num[0]; // 1+45+5=51
System.out.println(total);
for(int index=0; index<=4; index++)
System.out.println("The number at index " + index + " is " + num[index]);
}
Code for method StringArray():
void StringArray()
{
String word[] = new String[3]; //creates a String array object with indexes from 0 to 2
for(int i=0; i<=2; i++)
{
System.out.print("Enter word " + i + ": ");
word[i] = in.next();
}
System.out.print("The words you entered are: ");
for(int y=0; y<=2; y++)
System.out.print(word[y] + " ");
}