Data Structures
Lesson 1 of 1
  1. 1
    In the previous lesson, you learned a few of the object-oriented programming concepts used in Java: classes, objects, methods, and inheritance. In this lesson, you will learn how to use some of the…
  2. 2
    When you provide a set of instructions in a method, you might find that a common task is to manipulate an entire set of data. Java conveniently provides control statements to run a task repeatedl…
  3. 3
    Fantastic! For loops will be useful when reading and manipulating Java data structures. Let’s talk about a few data structures that are built into Java. The first data structure we will explore is …
  4. 4
    You created an ArrayList in the last exercise to store temperature data, but it does not contain any values yet. Let’s add some values of type Integer to the ArrayList, one by one, like this: Arr…
  5. 5
    Great! Now we can start accessing the temperatures that our ArrayList stores. We can access the elements of weeklyTemperatures by using an element’s index, or position, in the list. An element…
  6. 6
    We can also insert new elements into an ArrayList. To insert new elements into an ArrayList, we can use a slightly different version of the add method that you previously used: ArrayList quizGra…
  7. 7
    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. for (int i = …
  8. 8
    Since most for loops are very similar, Java provides a shortcut to reduce the amount of code required to write the loop called the for each loop. Here is an example of the concise *for each loop…
  9. 9
    Great! 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 di…
  10. 10
    Perfect! Now let’s fill the HashMap with useful data. Add keys and values to a HashMap: HashMap myFriends = new HashMap (); myFriends.put(“Mark”, 24); myFriends.put(“Cassandra”, 25); myFriends….
  11. 11
    To access data in an ArrayList, we specified the index. In order to access a value in a HashMap, we specify the key: HashMap myFriends = new HashMap (); myFriends.put(“Mark”, 24); myFriends.put(…
  12. 12
    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: HashMap myFriends =…
  13. 13
    Great work! What did we learn so far? - For Loops: used to repeatedly run a block of code - For Each Loops: a concise version of a for loop - ArrayList: stores a list of data - HashMap: st…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo