CS201 - Spring 2014 - Class 16

  • Bailey book

  • ArrayList vs. Vector

  • Generics:
       - Recall from our discussion of ArrayList that generics allow us to allow a class to be instantiated with many different types
       - For example, it allows us to have ArrayLists of many different types:
          ArrayList<Integer> nums = new ArrayList<Integer>();
          ArrayList<String> strings = new ArrayList<String>();

       - If you look at the documentation for ArrayLists, you can see that they have a generic type argument, E:
          http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

          - The "type variable" E, is used anywhere that a type would be expected
          
       - We can write our own classes that use generics!
       - Look at Container class in GenericsExamples code
          - We can specify one or my type variables in the class header inside < >
             public class Container<V>{
       
             }

             - by convention, we use a single uppercase letter to indicate a type variable
          - You can then use this variable anywhere in your program where a type would be used
             - private V value;
             - public Container(V value)
             - public V getValue()
             - public void setValue(V newValue)
       - When you instantiate this class you should specify a type parameter
          Container<Integer> c = new Container<Integer>();
          int val = c.getValue();

          or

          Container<String> c = new Container<String>();
          String val = c.getValue();

          - For example, we could add the main:

             Container<String> c1 = new Container<String>("banana");
             Container<Integer> c2 = new Container<Integer>(10);
          
             System.out.println(c1.getValue());
             System.out.println(c2.getValue());
          
             // c2.setValue("car"); // DOESN'T WORK!
       - How can we return multiple objects/data types from a method in java?
          - The bad way: public void Object[] method()
          - The good way: implement a Pair class using generics
       - implement a class called Pair that supports generics for 2 types
          - if you need more than one type variable, you can introduce another one and comma separate
          - What methods should the class contain?
          - Constructor(s)?
          - private instance variables?
          - methods?
       - look at the Pair class in the GenericsExamples code

  • "this" keyword
       - anytime we're in a non-static method, the method was called *on* an object, e.g., for our Container class
          Container<String> c = new Container<String>();

          c.getValue()

          - the getValue method is being called on the object referenced by c
          - this is exactly why we can reference instance variables inside of the class, they are c's copies of the instance variables
       - there are a few occasions where inside of the class, you might want to reference the current object. Any ideas?
          - one of the most common things to do in a constructor with a parameter is to save that parameter away in an instance variable
          - rather than come up with two different names (one for the instance variable and one for the parameter), it would be nice to have them have the same name. What's the problem with this?
              - we won't be able to reference both, only the parameter
       - the "this" keyword gives us a reference to the current object, that is, the object that this method was called on
       - One of the most common things to do with "this" is to set an instance variable with a parameter with the same name
       - look at the Pair class in the GenericsExamples code
          - in the constructor, we said:
             this.first = first;

             - this.first is the instance variable first
             - first is the parameter
          - what would have happened if we'd said:
             first = first?
             
             - Eclipse warns us that the assignment has no effect
             - we're just setting a variable to itself
       - the other time you might use "this" is if you wanted to add call a constructor within a class. For example, say we want to provide a zero parameter constructor for our Card class that be default creates the 2 of clubs. How would you write it?
          - Approach 1:

             public Card(){
                number = 2;
                suit = "clubs";
             }

          - Approach 2 (a better approach): use the existing constructor!
             public Card(){
                this(2, "clubs");
             }

             - Why is this approach better?
                - Code reuse!
                - any additional functionality that we have in the main constructor gets utilized
                   - in this case, it doesn't add much value
                   - though it does check that our parameters are valid, just in case