GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 22: Generics and Data Structures.


Objectives

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




Before Starting

If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:




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.

Activity 1 [Group Work]: 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?

      FBUser[] friends = new FBUser[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!).

However, we don't know how to write a class that can store data of a type that will be later specified by a 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?

Activity 2 [Group Work]: Write a program that creates an ArrayList of Strings, put three strings into it, and then print out the contents of the ArrayList as well as its size.

Activity 3 [Group Work]: Add code to your program to do the following: For each of these, use no loops and only use the ArrayList methods.




Other Data-Structures

Check out the LinkedList Javadoc.

Activity 4 [Group Work]: Questions -



Activity 5 [Group Work]: What is the difference between arrays, ArrayLists, and LinkedLists? Why does Java have three implementations of ways of storing data?

An aside aboout class-types for primitives

It turns out that all the familiar oprimitives (int, double, boolean, char, etc) have Class types that define some methods and properties for peerforming high level tasks with them.

We've used these when converting to and from string:

public class CopyExample3 {

  public static void main (String[] argv)
  {
    String s = "123";
    int num = Integer.parseInt(s);
    System.out.println (num + 4);
  }

}
This code willl print a 127 rather than 1234.

When using generics:
If you want to use generics with "primitives", you must use the Class type instead of the primitive verision.

Activity 6 [Group Work]: Add the following code to GenericsExample.java. Then, 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 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.

Activity 7 [Group Work]: Questions -



Activity 8 [Group Work]: 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

 


Activity 9 [Group Work]: 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.