GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 10: Generics and Data Structures.


Objectives

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




Why are data structures useful in computation?

Last clss, we talked about how Java allows developers to create their own types using classes. Java also has built-in classes, such as String that have already been defined, can be used by developers as types.

Each time someone declares a new class, recall that it is saved in a file with the name of that class. We've seen examples of one Java class (with a main method) using a class that was defined in another file. What we haven't explained yet, is how Java knows where to look on the operating system for these other classes. There are three options we'll cover:

.

Next, let's take a look at a very popular Java class in the java.utils library.

.

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:

While arrays are great, we saw many examples where it was tedious to specify the size of the array up-front, and not be able to grow or shrink the list dynamically. Java has a class called ArrayList in the java.utils library, that's basically a wrapper around such primitive arrays, but allows them to be added to and removed from after they've been created (unlike primitive arrays).

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.




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.



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

Activity 4 [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));


HashMaps

Check out the HashMap Javadoc.

Activity 5 [Group Work]: Questions -



Activity 6 [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;
  }
}




Next class:

We'll begin the NLP in-class project. Please come, in person, to every lecture and lab for the rest of the semester (except the Tues before Thanksgiving -- you can attend remote).

Assignments for next lecture:

Read over Part 1 of the NLP in-class project, and email your partner.