dsa

Data Structures & Algorithms - Spring 2018
Log | Files | Refs | README

ListArrayListBased.java (1486B)


      1 package lab2;
      2 
      3 import java.util.ArrayList;
      4 
      5 public class ListArrayListBased implements ListInterface{
      6 
      7     protected ArrayList<Object> items;  // an array list of objects
      8 
      9     public ListArrayListBased()
     10     {
     11         items = new ArrayList<Object>();
     12     }  // end default constructor
     13 
     14     public boolean isEmpty()
     15     {
     16         return (items.isEmpty());
     17     } // end isEmpty
     18 
     19         public int size()
     20     {
     21         return items.size();
     22     }  // end size
     23 
     24     public void removeAll()
     25     {
     26         items.clear();
     27     } // end removeAll
     28 
     29     public void add(int index, Object item)
     30             throws  ListIndexOutOfBoundsException
     31     {
     32         items.add(index, item);
     33     } //end add
     34 
     35     public Object get(int index)
     36             throws ListIndexOutOfBoundsException
     37     {
     38         if (index >= 0 && index < size())
     39         {
     40             return items.get(index);
     41         }
     42         else
     43         {
     44             // index out of range
     45             throw new ListIndexOutOfBoundsException(
     46                     "ListIndexOutOfBoundsException on get");
     47         }  // end if
     48     } // end get
     49 
     50     public void remove(int index)
     51             throws ListIndexOutOfBoundsException
     52     {
     53         if (index >= 0 && index < size())
     54         {
     55             items.remove(index);
     56         }
     57         else
     58         {
     59             // index out of range
     60             throw new ListIndexOutOfBoundsException(
     61                     "ListIndexOutOfBoundsException on remove");
     62         }  // end if
     63     } //end remove
     64 }
     65