Data Structures

For Loop

The for loop repeatedly runs a block of code until a specified condition is met.

The example below shows how a for loop is used:

1
2
3
4
5
for (int counter = 0; counter < 5; counter++) {

    System.out.println("The counter value is: " + counter);

}

The statements within the parentheses of for loop compose the following parts:

  1. Initialization: the int variable named counter is initialized to the value of 0before the loop is run.

  2. Test condition: the Boolean expression counter < 5 is a conditional statement that is evaluated before the code inside the control statement is run every loop. If the expression evaluates to true, the code in the block will run. Otherwise, if the expression evaluates to false, the for loop will stop running.

  3. Increment: Each time the loop completes, the increment statement is run. The statement counter++ increases the value of counter by 1 after each loop.

ArrayList

The ArrayList stores a list of data of a specified type. Let’s go through an example of how to create, or declare, an ArrayList of type Integer.

1
2
3
import java.util.ArrayList;

ArrayList<Integer> quizGrades = new ArrayList<Integer>();

ArrayList is a pre-defined Java class. To use it, we must first create an ArrayList object.

In the example above, we create an ArrayList object called quizGrades that will store data types belonging to the <Integer> class (whole numbers).

Manipulation

Let’s add some values of type Integer to the ArrayList, one by one, like this:

1
2
3
4
ArrayList<Integer> quizGrades = new ArrayList<Integer>();
quizGrades.add(95);
quizGrades.add(87);
quizGrades.add(83);

In the example above, we call the addmethod on quizGrades. The add method adds integers to the ArrayList. The values 95, 87, and 83 are added to the list.

Access

Here is an example of accessing the element in the first position of the list:

1
2
3
4
5
6
ArrayList<Integer> quizGrades = new ArrayList<Integer>();
quizGrades.add(95);
quizGrades.add(87);
quizGrades.add(73);

System.out.println( quizGrades.get(0) );

The example above will print out the grade value of 95.

Insertion

To insert new elements into an ArrayList, we can use a slightly different version of the addmethod that you previously used:

1
2
3
4
5
6
7
ArrayList<Integer> quizGrades = new ArrayList<Integer>();
quizGrades.add(95);
quizGrades.add(87);
quizGrades.add(73);

quizGrades.add(0, 100);
System.out.println( quizGrades.get(0) );

Iterating over

Earlier in this lesson, we learned about the for loop. Since we’ve also learned how to retrieve the value at a specific index of an ArrayList, we can now access each of the elements.

1
2
3
4
5
for (int i = 0; i < quizGrades.size(); i++) {

    System.out.println( quizGrades.get(i) );

}

For Each Loop

Here is an example of the concise for each loop:

1
2
3
for (Integer grade : quizGrades){
    System.out.println(grade);
}

HashMap

Another useful built-in data structure in Java is the HashMap.

Although the name of the data structure might not make sense to you immediately, think of it as a real-life dictionary. A dictionary contains a set of words and a definition for each word. A HashMap contains a set of keysand a value for each key.

If we look up a word in a dictionary, we can get the definition. If you provide a HashMap with a key that exists, you can retrieve the value associated with the key.

Declaring a HashMap is shown in the following example:

1
2
3
import java.util.HashMap;

HashMap<String, Integer> myFriends = new HashMap<String, Integer>();

Manipulation

Add keys and values to a HashMap:

1
2
3
4
5
HashMap<String, Integer> myFriends = new HashMap<String, Integer>();

myFriends.put("Mark", 24);
myFriends.put("Cassandra", 25);
myFriends.put("Zenas", 21);

Access

To access data in an ArrayList, we specified the index. In order to access a value in a HashMap, we specify the key:

1
2
3
4
5
6
7
HashMap<String, Integer> myFriends = new HashMap<String, Integer>();

myFriends.put("Mark", 24);
myFriends.put("Cassandra", 25);
myFriends.put("Zenas", 21);

System.out.println( myFriends.get("Zenas") );

Iterating over

We can also access properties of a HashMap, such as the number of entries or the contents of the HashMap.

Let’s access the length and print out the contents of the myFriends:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
HashMap<String, Integer> myFriends = new HashMap<String, Integer>();

myFriends.put("Mark", 24);
myFriends.put("Cassandra", 25);
myFriends.put("Zenas", 21);

System.out.println( myFriends.size() );

for (String name: myFriends.keySet()) {

    System.out.println(name + " is age: " + myFriends.get(name));

}