CS201 - Spring 2014 - Class 14
exercises
Admin
- Assignment 5 part 2 out today
- you may work with a partner (and should!)
- technically not due until Friday at 5pm
- get 5% extra credit for turning it in at the normal time on Wed.
What's one of the biggest downsides/annoyances with arrays?
- fixed size: you need to know a priori the size of the arrays
Lists
- You've all seen lists in some context before
- What are they? How are they different than arrays?
- A list stores data items sequentially
- It allows you to access them via their index
- Unlike arrays, they can be extended!
- What type of functionality would you like to have from lists?
- get
- set
- add
- size
- insert
- sort?
- clear
- remove
- Java has a List interface (
http://docs.oracle.com/javase/7/docs/api/java/util/List.html
)
- specifies a lot of the methods that we'd like to have for lists
- Why an interface?
- there are many different ways that we could support this behavior!
- we'll see a couple of different ones in this class
Why aren't arrays expandable?
- arrays are designed to have indexing be fast
- to do this, they're laid out as a continuous block of memory
- increasing this would require
1) the adjacent blocks to be empty (which generally won't be the case)
2) leaving open space adjacent. This is wasteful and still puts a limit on how much we can expand.
3) create space somewhere else. This solves our expanding problem, but then our array will be spread out in multiple places, requiring that we look for which slot when we want to find a particular index.
How could we implement this functionality?
java.util.ArrayList (
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
)
- implements the List interface
- we'll talk next time why it's called "ArrayList"
- any guesses?
- it's a class, just like everything else we've seen
- It defines a type
- It has a constructor (actually, multiple)
- once instantiated, it has methods
list.add(10);
int num = list.get(0);
generics (or parameterized data types)
- What types of things would we like to be able to store in ArrayLists?
- ideally, anything
- What should be the return type of get? What should be the parameter type of methods like add or insert?
- We'd like to be able to store lots of different things
- To do this, we could make get return an Object and have add take an Object as a parameter
- Any downsides to this?
- Most of the time, like with arrays, we're going to store the same type of thing
- an ArrayList of number
- an ArrayList of Strings
- an ArrayList of Cards
- etc.
- If we have the ArrayList returning and taking Objects we lose a lot of the benefits of static typing!
- we'd have to cast every object that was returned into what we thought we'd stored
- don't get any of the nice checks at compile-time
- more error prone because we could accidentally put something into the array that we thought was of the right type
- For example:
ArrayList list = new ArrayList();
list.add("some string");
String s = (String)list.get(0)
- Or:
ArrayList list = ArrayList();
list.add("some string");
// a bunch of code here
int num = (int)list.get(0); // this may or may not be a number!
- when would we catch this error?
- Not until the code was run!
- What is polymorphism?
- poly, means many
- morphism means shape or form (e.g. metamorphism, isomorphism, dimorphism)
- In CS, polymorphism refers to things that can serve multiple functions
- in our case, we'd like to have polymorphic typed ArrayList:
- they have the same interface
- but work on many different types of data
- When creating an ArrayList we can specify the type that will be stored in the ArrayList with <>!
- For example:
ArrayList<String> list = new ArrayList<String>();
list.add("some_string");
String s = list.get(0);
- By defining it this way, we specify the type of the parameter that the ArrayList will work on
- If you look at the ArrayList documentation, you'll see that it says "ArrayList<E>"
- E is what is called a "type" parameter
- It can be any class type
- If you look at the method definitions, they then use E in places a type would be expected
- add(E e)
- E is the type
- e is the variable
- when you instantiate the ArrayList, you specify the type and then the type of the method would become whatever specified
- Would the following example work?
ArrayList<String> list = ArrayList<String>();
list.add("some string");
// a bunch of code here
int num = list.get(0);
- NO!
Integer, Double, Float, Boolean,
- generics require that the type be a class, so the 8 built-in types will not work!
- However, there are variants implemented of all of these classes for you!
- int -> Integer
- double -> Double
- boolean -> Boolean
- ...
Compare ArrayExamples from
ArrayExamples code
to ArrayListExamples from
ArrayListBasics code
- need to import java.util.ArrayList to use it
- try and write sumArray using an ArrayList
- notice that most of these methods don't change except for the parameter types!
Compare CardDealer from
ClassBasics code
vs. CardDealerArrayList from
ArrayListBasics code
Look at MoreArrayListExamples in
ArrayListBasics code
- both these methods compile
- will they run?
- trouble?
- No!
- When ArrayLists are created, they're created empty, that is with size = 0
- You cannot set a value that does not exist already
- trouble2?
- ArrayList does have a constructor that takes a size
- but the ArrayList STILL starts out empty and with size = 0
- You must "add" an element to an ArrayList to increase the size, then you can access elements