Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎

2018-11-01 Extending the ArrayList

posted Nov 1, 2018, 6:03 AM by Konstantinovich Samuel   [ updated Nov 2, 2018, 7:58 AM ]
Goal : Generics + Extend the Array List


Generics:

Using the format
public class ClassName<VARIABLE_TYPE>{

}

Restriction: It must be an OBJECT type, no primitives!
You may now use that VARIABLE_TYPE as a type throughout your code.


public class ThingThatHolds<T>{
public T value;
}



WITHOUT METHODS (for demo only)

public
class Holder<T>{ public T value; public static void main(String[]args){ Holder<String> name = new Holder<String>(); name.value = "Whoa!"; Holder<Integer> data = new Holder<Integer>(); data.value = 5; System.out.println(name.value+" "+data.value); } }


WITH METHODS:

public class Holder<T>{ private T value;

 public Holder(){ //NOTICE NO <> here!!! }
 public Holder(T start){ //NOTICE NO <> here!!! value = start; } public T getValue(){ return value; } public void setValue(T newVal){ value = newVal; } public static void main(String[]args){ Holder<String> name = new Holder<String>(); name.setValue("Whoa!"); Holder<Integer> data = new Holder<Integer>(); data.setValue(5); System.out.println(name.getValue()+" "+data.getValue()); } }





Consider the Class Hierarchy Below

ArrayList (built in class - your super array)
\
NoNullArrayList (not built in)
\
             OrderedArrayList (not built in)

Lab (Due Monday) Implement the NoNullArrayList and OrderedArrayList.
COMPLETE NoNullArrayList before moving on to OrderedArrayList

Github repo: MKS21X-OrderedArrayList

Note: 
-There are many methods in the arraylist that can break the NoNullArrayList and OrderedArrayList.
-If you do not use addAll, or the constructor(Collection<>) then you should be fine. 
-DO NOT use any built in sort methods!


NoNullArrayList<T> extends ArrayList<T>
    is an ArrayList of T that cannot take a null as a value.

    Override set and both add methods -> throw an IllegalArgumentException when null is added,
                                 otherwise call the old add method.


    Only have 2 constructors: default + startingCapacity.
    

OrderedArrayList<T extends Comparable<T>> extends NoNullArrayList<T>
    This is a NoNullArrayList that maintains all elements in a sorted order upon insertion.

    Override both add methods -> find the correct index where the new element should be added, 
                                 then add() the new element into that position
                                 (hint: 1.compareTo 2.helper method)

    Override set(index,value) -> remove() the element at index, then add() the new value.

    Only have 2 constructors: default + startingCapacity.



There are three main tricky parts to this assignment:
1) finding the correct index to add to in OrderedArrayList is the obvious one.
2) using the super methods at the correct time (e.g. in ordered array list, once you know where to put it, use the super.add method)
3) How to make it compile properly... because we want our list to accept a generic type.

Comments