Tuesday, April 3, 2007

Lecture 26

Linked Lists Continued

Node Inner Classes

  • ListNode has no use or meaning apart from the linked list
  • our existing definition opens the list to privacy leaks
  • a careless application programmer could get a reference and change it
  • make ListNode an inner class within the 'linked list' class
    • advantages
      • eliminates privacy leaks
      • simplifies LinkedList
    • disadvantages
      • reduces flexibility
Example

public class LinkedList inner
{
private class ListNode // inner class
{
private myObject item;
private ListNode next;
public ListNode(MyObject item, ListNode next) { ... }
}
private listNode head; ...}

  • using myObject as a generic
    • linked lists with generics
public class LinkedList inner //good

{
private class ListNode /** this is good, but this alone does not suffice */
{
private T item;
private ListNode next; // bad
private ListNode next; //good

  • use the generic type parameter firstly with the outer class as well as the inner class
  • use the generic type parameter with every declaration of the generic object.

No comments: