ADT ArrayList

  1 // File:  ADTList\NItem.java 
  2  
  3 /** 
  4  * Realization of a List ADT 
  5  *  
  6  * @author Bary W Pollack 
  7  * @version Dec. 31, 2000 - 23:00 PST 
  8  */ 
  9  
 10 // Represents an "Item" that contains an int 
 11 public class NItem extends Item { 
 12  
 13     // Default constructor 
 14     NItem() { } 
 15  
 16     // Loads the data item 
 17     NItem(int nItem) { 
 18         this.nItem = nItem; 
 19     } 
 20  
 21     // Accessor - returns the data item 
 22     public int getNitem() { 
 23         return nItem; 
 24     } 
 25  
 26     // String representation of the data 
 27     public String toString() { 
 28         return "" + nItem; 
 29     } 
 30  
 31     private int nItem; 
 32  
 33 } 

  1 // File:  ADTList\ArrayList.java 
  2  
 10 // An array implementation of a list of ints 
 11 public class ArrayList extends List { 
 12  
 13     // Default constructor 
 14     ArrayList() { } 
 15  
 16     // Creates a fresh array-implemented list of 
	  // a given length 
 17     public void create(int length) { 
 18         list = new NItem[length]; 
 19         this.length = 0; 
 20     } 
 21  
 22     // True if the list cannot contain any more Items 
 23     public boolean isFull() throws ListException { 
 24         checkNullList(); 
 25         return (length >= list.length); 
 26     } 
 27  
 28     // Returns the number of items in the list 
 29     public int size() throws ListException { 
 30         checkNullList(); 
 31         return length; 
 32     } 
 33      
 34     // Adds the item at location index, 
	  // moving the rest down 
 35     public void add(int index, NItem nItem) 
					throws ListException { 
 36         checkNullList(); 
 37         if (index < 0 || index > length 
					|| index >= list.length) 
 38             throw new ListException(
			 "index out of range (" + index + ")"); 
 39         if (length >= list.length) 
 40             throw new ListException(
			 "list full (" + length + ")"); 
 41         for (int i = length-1  +1; i > index; i--) 
 42             list[i] = list[i-1]; 
 43         list[index] = nItem; 
 44         ++length; 
 45     } 
 46  
 47     // Removes the item at list[index] 
 48     public void remove(int index) 
					throws ListException { 
 49         checkNullList(); 
 50         checkIndex(index); 
 51         if (index >= length) 
 52             throw new ListException(
			"index out of range (" + index + ")"); 
 53         for (int i = index; i < length-1; i++) 
 54             list[i] = list[i+1]; 
 55         --length; 
 56     } 
 57  
 58     // Empties the list 
 59     public void removeAll() { 
 60         create(list.length);  
 61     } 
 62  
 63     // Retrieves a copy of the NItem at list[index] 
 64     public Item get(int index) throws ListException { 
 65         checkNullList(); 
 66         checkIndex(index); 
 67         return list[index]; 
 68     } 
 69  
 70     // Displays the list on System.out 
 71     public void display() throws ListException { 
 72         checkNullList(); 
 73         System.out.println(toString()); 
 74     } 
 75  
 76     // Checks to ensure that 'list' is not null 
 77     protected void checkNullList() 
					throws ListException { 
 78         if (list == null) 
 79             throw new ListException(
					"list is <null>"); 
 80     } 
 81  
 82     protected NItem[] list; 
 83     protected int length; 
 84 } 

  1 // File:  ADTList\ArrayListDemo.java 
  2  
 10 // A simple demonstration of using the List class 
 11  
 12 public class ArrayListDemo { 
 13  
 14     static final int LTH = 5; 
 15  
 16     public static void main(String[] args) { 
 17         System.out.println(); 
 18         System.out.println("Array List Demo"); 
 19         System.out.println(); 
 20  
 21         ArrayList list = new ArrayList(); 
 22         list.create(LTH); 
 23  
 24         try { 
 25             // Force an exception by going one too far... 
 26             for (int i = 0; i < LTH + 1; i++) { 
 27                 list.add(i, new NItem(11 + i)); 
 28                 System.out.println("size=" 
				+ list.size() + "  " + list); 
 29             } 
 30         } catch (ListException e) { 
 31             System.out.println(e); 
 32         } 
 33  
 34         System.out.println(); 
		System.out.print("AL display: "); 
 35         list.display(); 
 36         System.out.println("isFull=" 
				+ list.isFull()); 
 37  
 38         list.remove(list.size()/2); 
 39         System.out.println("size-" 
				+ list.size() + "   " + list); 
 40         list.remove(0); 
 41         System.out.println("size=" 
				+ list.size() + "   " + list); 
 42         list.remove(list.size() - 1); 
 43         System.out.println("size=" 
				+ list.size() + "   " + list); 
 44         System.out.println("isFull=" 
				+ list.isFull()); 
 45  
 46         System.out.println(); 
 47         System.out.println("End Array List Demo"); 
 48         System.out.println(); 
 49     } 
 50  
 51 } 
 52  
 53 /* ..... Execution Output ..... 
 54  
 55 Array List Demo 
 56  
 57 size=1  11 
 58 size=2  11, 12 
 59 size=3  11, 12, 13 
 60 size=4  11, 12, 13, 14 
 61 size=5  11, 12, 13, 14, 15 
 62  
 63 ******************** BACKTRACE ******************** 
 64 ArrayList.java, Compiled Code  from ArrayList.add() 
 65 ArrayList.java, Compiled Code  from ArrayList.add() 
 66 ArrayListDemo.java, Compiled Code  from 
						ArrayListDemo.main() 
 67 ******************** EXCEPTION ******************** 
 68 ListException: index out of range (5) 
 69  
 70 AL display: 11, 12, 13, 14, 15 
 71 isFull=true 
 72 size-4   11, 12, 14, 15 
 73 size=3   12, 14, 15 
 74 size=2   12, 14 
 75 isFull=false 
 76  
 77 End Array List Demo 
 78  
 79 ..... End Execution Output ..... */