Module 12: Data-Structures and Generics


Objectives

 

By the end of this module, you will be able to:

 


ArrayList

 

Data-structures are means for organizing and accessing data. They are one of the main topics in CSCI1112.

Examples we've seen so far:

Please take a few minutes to browse the ArrayList Javadoc page.

 

In-Class Exercise 1: Questions -

 

Generics

Data-structures contain data. We want the type of the data to be different, depending on what data we're organizing

How do we have different arrays containing different types of data?

      OSMRoad[] roads = new OSMRoad[n];
      double[][] spreadsheet = new double[4][10];
      String[] sentences = new String[numSentences];

More complex data-structures must be implemented in Java classes (often by us!). We don't know how to write a class that can store data of a type specified by the client, or user of our class.

Generics enable us to write a class that stores data of a type specified by the user of our class.

What do generics look like?

 

In-Class Exercise 2: Write a program that creates an ArrayList of Strings, put three strings into it, and then print out the times in the ArrayList.
 

In-Class Exercise 3: Add 5 strings to your ArrayList and do the following:

For each of these, use no loops and only use the ArrayList methods.
 


Other Data-Structures

 
 

Check out the LinkedList Javadoc.
 

In-Class Exercise 4: Questions -

  • How do you add values to a linked list?
  • How do you find the value at an index?
  • Having a little Deja vu? ("It happens when they change something.")
 

In-Class Exercise 5: What is the difference between arrays, ArrayLists, and LinkedLists? Why does Java have three implementations of ways of storing data?
 


An aside: primitive types (non-reference types) and Generics

If you want to store "int"s, or any other primitive type, you have to specify the type as "Integer". This is a Java-ism that you'll understand later. Review Zybooks for reference vs. primitive types.

 

In-Class Exercise 6: Compile the following code. Fix the code so that it compiles and runs.

ArrayList<int> al = new ArrayList<int>();
al.add(10);
System.out.println(al.get(0));

 

Note that there are corresponding types for

  • double → Double
  • float → Float
  • boolean → Boolean
Using these classes, Java automatically converts code such as

ArrayList<Integer> al = new ArrayList<Integer>();
al.add(10);

to

ArrayList<Integer> al = new ArrayList<Integer>();
al.add(Integer(10));


 

Check out the HashMap Javadoc.
 

In-Class Exercise 7: Questions -

  • What does the <K,V> mean in "Class HashMap<K,V>"?
  • The main operations for a HashMap are "get" and "put". What do you think a HashMap does? What is its main use?
  • How is it different from LinkedList and ArrayList?
 

In-Class Exercise 8: Write a program for the IRS that lets us store people, and later look them up by their social security number (SSN). Use the person class below. Add three people objects to a HashMap, using their social security number as the key. Look them up, and print out their names.

public class Person {
	private String name;
	private int    ssn;
	public Person(String n, int ssn) {
		name = n;
		this.ssn = ssn;
	}
	public String getName() {
		return name;
	}
	public long getSsn() {
		return ssn;
	}
}

 


JavaDoc Treasure Hunt

 
 

In-Class Exercise 9: Write a program that

  1. Creates an HashMap of People.
  2. Populates it with a number of people (key = ssn, value = Person object).
  3. Creates an ArrayList with the same contents as all of the values in the HashMap.
  4. ...without using loops. It should work just the same if the HashMap has 3 People, or 3 million.