Constructing Lists of Different Types

Suppose we want a list of Strings instead of a list of ints

 

In Java, String is a type defined in a package java.lang.

Strings are objects (int's are not objects).

 

The construction is nonetheless similar: we just store a String in the list cell rather than an int.

We name this type StringList in contrast to IntList.

To create StringList, given IntList, simply use a text editor:

Link to StringList.java which shows the result.

What is actually being stored is a reference to a String. This allows all such cells to have the same size.

 

Were we to instead store the characters of the string in the cell directly, each cell might have a different size. This is ok, but it would create additional management problems for us.

 

Since String is a standard type, it is simpler to pass off such management aspects to the String type itself.

Next Slide Previous Slide Contents