11-20-2013

posted Nov 19, 2013, 11:33 PM by Samuel Konstantinovich   [ updated Nov 19, 2013, 11:34 PM ]
Goal: Work while Mr. K has jury duty. 

Since I found out about the jury duty so late, I didn't have time to properly prepare good lecture notes. 

This is a good page for WHY we want to use generics. 

We could use an ArrayList of objects, WITHOUT generics, and it would require us to TYPECAST every time we get an element from the list. 

ArrayList can store Integers and other things as well. This would require casting.
ArrayList<Integer>can ONLY store Integers, but does not require casting.

But honestly at 2am, I cannot really come up with a great tutorial, and I don't like the other tutorials that exist. I just cut/paste the parts I liked from several sources, to give you an idea how it should look, and you should NOW make a copy of your MyStringArray.java called SuperArray.java. The SuperArray is going to be like the ArrayList but you will have written it yourself. You will make it work with any objects!


Here is an example of a generic "box" that contains 1 thing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class GenericBox<E> {
   // Private variable
   private E content;
 
   // Constructor
   public GenericBox(E content) {
      this.content = content;
   }
 
   public E getContent() {
      return content;
   }
 
   public void setContent(E content) {
      this.content = content;
   }
 
   public String toString() {
      return content + " (" + content.getClass() + ")";
   }
}
Notice that the letter E stands for whatever type the person wants. 
When you declare this object you would say:
GenericBox<String> b = new GenericBox<String>(); // Now the E's in your class are all treated like String!
1
2
3
4
5
6
7
8
9
10
11
12
13
public class TestGenericBox {
   public static void main(String[] args) {
      GenericBox<String> box1 = new GenericBox<String>("Hello");
      String str = box1.getContent();  // no explicit downcasting needed
      System.out.println(box1);
      GenericBox<Integer> box2 = new GenericBox<Integer>(123);  // autobox int to Integer
      int i = box2.getContent();       // downcast to Integer, auto-unbox to int
      System.out.println(box2);
      GenericBox<Double> box3 = new GenericBox<Double>(55.66);  // autobox double to Double
      double d = box3.getContent();     // downcast to Double, auto-unbox to double
      System.out.println(box3);
   }
}
So now you have to make your MyStringList class have an array of things... this means you need to use an array of OBJECTS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Start with this:
public class SuperArray<E> {
   private int size;     // number of elements
   private Object[] elements; // Array of objects
   
   public SuperArray() {  //constructor
      /*implementation not shown*/
} public void add(E e) { //notice what an accessor returns /*implementation not shown*/ } //The get method returns a Type-casted value. This is the way we get around doing it later! public E get(int index) { return (E)elements[index]; } public int size() { return size; }
}



Comments