Introduction to Software Development
GWU Computer Science
By the end of this module, you will be able to:
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:
import
these libraries at the top
of our files.String
, don't require an import
statement to use them.Next, let's take a look at a very popular Java class in the java.utils
library.
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 -ArrayLists are similar to arrays. How do we find the value at an index into the ArrayList?
How do we add/change a value at a specific index?
What is the equivalent of array's ".length"?
If we want to get the data in the ArrayList in an array?
What if we want to do something similar to String's ".substring" method?
What do you think that this
means: public class ArrayList<E>
What is the "<E>"?
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?
public class ArrayList<E>
E is the type of data we want to hold in the ArrayList
Creating an ArrayList that holds Strings:
ArrayList<String> sentences = new ArrayList<String>();
Note: we are specifying that we want to create an array list of strings
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.
ArrayList<int> al = new ArrayList<int>();
al.add(10);
System.out.println(al.get(0));
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(10);
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(Integer(10));
Check out
the HashMap Javadoc.
Activity 5 [Group Work]: Questions -
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;
}
}