In our day-to-day lives we often encounter the need to make lists. For example, if we are packing for a trip we can make a packing list; if we are going to the grocery store, we may bring a grocery list. In this lesson, we will start looking at how we can use lists in our programs.
Anything that can store data can be called a data structure. Hence, integer, boolean, char, double etc, are all data structures. They are known as Primitive Data Structures. In this lesson we will explore a new type of data structure which will allow us to store data as a list. This type of data structure is referred to as an array
An array is a specific data structure that stores data as a list
One way we can visualize an array, is to imagine a street of houses. Notice on our street below, each house has an address and that the addresses start at "0". Each address in our example is also referred to as an index. The contents inside each house are referred to as the value associated with the index.
index refers to the location of an element in an array.
value refers to the contents stored at the index.
An array in java can be declared, initialized, and populated in at least two ways. Each are described below.
Method 1The code snippet below illustrates one method for declaring, initializing, and populating an array. This method is only useful if we know the identity of the elements we want to store.
String houses[] = {"Bart", "Kyle", "Bugs", "Marvin"};
The code snippet below illustrates another method for declaring an array. In the below example the array is only declared, not initialized.
String houses[];
To initialize the array we use the new keyword to tell java this is a new array. Additionally, we need to tell java the datatype the array will store, along with how many items the array can hold. The code snippet below illustrates how to initialize the array we declared above to hold up to 4 items.
houses = new String[4];
While the array above has been declared and initialized, it currently does not hold any values. The below code snippet illustrates how to populate each location in the array with a value. If no value is specified, the location will be assigned the value null.
houses[0] = "Bart";
houses[1] = "Kyle";
houses[2] = "Bugs";
houses[3] = "Marvin";
While the above examples how to declare, initialize, and populate an array of String datatypes. Arrays for double, int, char, and boolean types are also possible. They are declared, initialized, and populated in exactly the same way. We can even make arrays of objects - but, more on that later!
Iterating over an array is the process of accessing each element of array one by one. Accessing each element in an array requires that we know both the address of the first element and the address of the last element in the array. The first element in an array is always located at index = 0, the last element of an array can be found if the length of the array is known. The syntax for finding the length of an array is illustrated below,
String houses = new String[4];
int addresses = houses.length;//4
In the illustration below, we see that the address of the last house in our array is one less than the length.
Consider the following code snippet,
houses[houses.length] = "Wirt";//Out of Bounds error!
houses[houses.length-1] = "Wirt";//Assigns Wirt to the last index
Now that we know the address of the first and last element, we can apply the loops we've learned previously to iterate over any array. In each example, we will iterate over the array below,
String houses = new String[4];
houses[0] = "Bart";
houses[1] = "Kyle";
houses[2] = "Bugs";
houses[3] = "Marvin";
for(int a = 0; a < houses.length; a++){
System.out.println(houses[a] + " lives in house " + a);
}
int address = 0;
while(address < houses.length){
System.out.println(houses[address] + " lives in house " + address);
address++;
}
int address = 0;
do{
System.out.println(houses[address] + " lives in house " + address);
address++;
}while(address < houses.length);
A for-each loop is another type of loop that allows us to access each element in an array. The syntax for a for-each loop is illustrated below,
for(String a:houses){
System.out.println(a);
}
The syntax above can be translated as follows: for each String "a" in houses and is described below,