9 genericsarrays fixed public class foot private t

This preview shows 9 out of 12 pages.

9 Generics/arrays, fixed public class Foo<T> { private T myField; // ok public void method1(T param) { myField = param; // ok T[] a2 = (T[]) (new Object[10]); // ok } } But you can create variables of that type, accept them as parameters, return them, or create arrays by casting Object[] .
Image of page 9

Subscribe to view the full document.

10 Comparing generic objects public class ArrayList< E > { ... public int indexOf( E value) { for (int i = 0; i < size; i++) { // if (elementData[i] == value) { if ( elementData[i].equals(value) ) { return i; } } return -1; } } When testing objects of type E for equality, must use equals
Image of page 10
11 Generic linked list nodes public class ListNode< E > { public E data; public ListNode<E> next; ... } For a generic linked list, the node class must also accept the type parameter E
Image of page 11

Subscribe to view the full document.

12 Generic interface (15.3, 16.5) // Represents a list of values. public interface List <E> { public void add( E value); public void add(int index, E value); public E get(int index); public int indexOf( E value); public boolean isEmpty(); public void remove(int index); public void set(int index, E value); public int size(); } public class ArrayIntList <E> implements IntList<E> { ... public class LinkedIntList <E> implements IntList<E> { ...
Image of page 12
You've reached the end of this preview.

{[ 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