List ADT Generic Array Implementation - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

List ADT Generic Array Implementation

Description:

Define classes that work with any type of object ... E set(int index, E entry); boolean add(E entry); boolean add(int index, E entry) ... – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 12
Provided by: bal1
Category:

less

Transcript and Presenter's Notes

Title: List ADT Generic Array Implementation


1
List ADTGenericArray Implementation
  • IC312

2
Array-based Listusing Objects
  • class CoinCounter
  • private MyList list
  • public CoinCounter(MyList l)
  • list l
  • public int run()
  • int total0
  • for (int i0 iltlist.size() i)
  • if (list.get(i) instanceof Coin)
  • Coin coin (Coin)list.get(i)
  • totalcoin.getValue()
  • return total

3
Generics
  • Define classes that work with any type of object
  • Eliminate the need to cast Objects to their
    underlying type
  • Let Java enforce restrictions on the type of
    objects
  • Enforcement occurs at compile time. Avoids
    runtime errors!

4
Coin Holder
  • public class Holder1
  • private Coin item1, item2
  • public Holder1(Coin it1, Coin it2)
  • item1 it1
  • item2 it2
  • public Coin getItem1()
  • return item1
  • public Coin getItem2()
  • return item2
  • public class HolderTest
  • public static void main(String args)
  • Holder1 h1 new Holder1(new Coin(1, 10),
    new Coin(2, 20))
  • Coin c1 h1.getItem1()
  • Coin c2 h1.getItem2()

5
Object Holder
  • public class Holder2
  • private Object item1, item2
  • public Holder1(Object it1, Object it2)
  • item1 it1
  • item2 it2
  • public Object getItem1()
  • return item1
  • public Object getItem2()
  • return item2
  • public class HolderTest
  • public static void main(String args)
  • Holder2 h2 new Holder2(new Coin(1, 10),
    new Coin(2, 20))
  • Coin c1 (Coin)h2.getItem1()
  • Coin c2 (Coin)h2.getItem2()

6
Generic Holder
  • public class Holder3ltEgt
  • private E item1, item2
  • public Holder3(E it1, E it2)
  • item1 it1
  • item2 it2
  • public E getItem1()
  • return item1
  • public E getItem2()
  • return item2
  • public class HolderTest
  • public static void main(String args)
  • Holder3ltCoingt h3 new Holder3ltCoingt(new
    Coin(1, 10),
  • new
    Coin(2, 20))
  • Coin c1 h3.getItem1()

7
Object List Interface
  • public interface List
  • int size()
  • boolean isEmpty()
  • Object get(int index)
  • Object set(int index, Object entry)
  • boolean add(Object entry)
  • boolean add(int index, Object entry)
  • Object remove(int index)

8
Generic List Interface
  • public interface ListltEgt
  • int size()
  • boolean isEmpty()
  • E get(int index)
  • E set(int index, E entry)
  • boolean add(E entry)
  • boolean add(int index, E entry)
  • E remove(int index)

9
Generic List Implementation
  • public class AListltEgt implements ListltEgt
  • private int size
  • private final int MAX_SIZE 50
  • private E array
  • public AListGeneric()
  • array (E)(new ObjectMAX_SIZE)
  • size 0
  • public int size()return size
  • public boolean isEmpty() return (size 0)
  • public E get(int index) throws
    IndexOutOfBoundsException
  • if (index gt 0 index lt size)
  • return arrayindex
  • else
  • throw new IndexOutOfBoundsException("index
    " index)

This syntax is a work around for arrays only.
Arrays are a special case when it comes to
generics.
10
Object Version of Remove
  • public Object remove(int index) throws
    IndexOutOfBoundsException
  • if (index gt 0 index lt size)
  • Object temp arrayindex
  • for(int i index i lt size-1 i)
  • arrayi arrayi1
  • arraysize-1 null
  • size--
  • return temp
  • else
  • throw new IndexOutOfBoundsException()

What does the generic version look like?
11
Generic Version of Remove
  • public E remove(int index) throws
    IndexOutOfBoundsException
  • if (index gt 0 index lt size)
  • E temp arrayindex
  • for(int i index i lt size-1 i)
  • arrayi arrayi1
  • arraysize-1 null
  • size--
  • return temp
  • else
  • throw new IndexOutOfBoundsException()
Write a Comment
User Comments (0)
About PowerShow.com