Public interface intlist public void addint value

Info icon This preview shows pages 4–9. Sign up to view the full content.

View Full Document Right Arrow Icon
public interface IntList { public void add(int value); public void add(int index, int value); public int get(int index); public int indexOf(int value); public boolean isEmpty(); public void remove(int index); public void set(int index, int value); public int size(); } public class ArrayIntList implements IntList { ... public class LinkedIntList implements IntList { ...
Image of page 4

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
5 Our list classes • We have implemented the following two list collection classes: – ArrayIntList – LinkedIntList – Problem: • They can store only int elements, not any type of value. 17 -3 42 value 2 1 0 index front 42 next data -3 next data 17 next data
Image of page 5
6 Type Parameters (Generics) ArrayList< Type > name = new ArrayList< Type >(); • Recall: When constructing a java.util.ArrayList , you specify the type of elements it will contain between < and > . We say that the ArrayList class accepts a type parameter , or that it is a generic class. ArrayList <String> names = new ArrayList <String> (); names.add("Marty Stepp"); names.add("Stuart Reges");
Image of page 6

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
7 Implementing generics // a parameterized (generic) class public class name < Type > { ... } By putting the Type in <> , you are demanding that any client that constructs your object must supply a type parameter. • You can require multiple type parameters separated by commas. The rest of your class's code can refer to that type by name. • Exercise: Convert our list classes to use generics.
Image of page 7
8 Generics and arrays (15.4) public class Foo<T> { private T myField; // ok public void method1(T param) { myField = new T(); // error T[] a = new T[10]; // error } } You cannot create objects or arrays of a parameterized type.
Image of page 8

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern