Advertisement
Guest User

DynamicArray.java

a guest
Aug 24th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package prove;
  2.  
  3. import java.util.Iterator;
  4. import java.util.NoSuchElementException;
  5.  
  6. import edu.princeton.cs.introcs.In;
  7. import edu.princeton.cs.introcs.StdOut;
  8.  
  9. public class DynamicArray<Item> implements Iterable<Item>, IterableCollection<Item> {
  10.     Item[] A;
  11.     int N; // Size of the array each moment. Different from capacity.
  12.    
  13.     @SuppressWarnings("unchecked")
  14.     DynamicArray() {
  15.         A = (Item[]) new Object[0];
  16.         N = 0;
  17.     }
  18.    
  19.     public void add(Item s) { // adds to the back
  20.        
  21.         if (A.length == 0)
  22.             resize(1);
  23.        
  24.         if (N == A.length) {
  25.             resize(2*A.length);
  26.             StdOut.println("Resized for " + s);
  27.         }
  28.         A[N++] = s;
  29.     }
  30.    
  31.     public void push_back(Item s) {
  32.         add(s);
  33.     }
  34.    
  35.     public Item get(int index) throws IndexOutOfBoundsException {
  36.         if (index < 0 || index > N)
  37.             throw new IndexOutOfBoundsException("Index out of bounds!");
  38.         return A[index];
  39.     }
  40.    
  41.     public void set(int index, Item value) throws IndexOutOfBoundsException {
  42.         if (index < 0 || index > N)
  43.             throw new IndexOutOfBoundsException("Index out of bounds!");
  44.         A[index] = value;
  45.     }
  46.    
  47.     public int capacity() {
  48.         return A.length;
  49.     }
  50.    
  51.     public int size() {
  52.         return N;
  53.     }
  54.    
  55.     public boolean isEmpty() {
  56.         return N == 0;
  57.     }
  58.    
  59.     public Item pop() {
  60.         Item s = A[--N];
  61.         A[N] = null; // Avoids loitering
  62.         if (N > 0 && N == A.length / 4)
  63.             resize(A.length / 2);
  64.         return s;
  65.     }
  66.    
  67.     private void resize(int capacity) {
  68.             @SuppressWarnings("unchecked")
  69.             Item[] newArray = (Item[]) new Object[capacity];
  70.             for (int i=0; i < N; ++i) {
  71.                 newArray[i] = A[i];
  72.             }
  73.             A = newArray;
  74.     }
  75.    
  76.     @Override
  77.     public Iterator<Item> iterator() {
  78.         return new DynamicArrayIterator();
  79.     }
  80.    
  81.     private class DynamicArrayIterator implements Iterator<Item> {
  82.         private int index;
  83.  
  84.         @Override
  85.         public boolean hasNext() {
  86.             if (index < size() - 1 ) return true;
  87.             return false;
  88.         }
  89.  
  90.         @Override
  91.         public Item next() {
  92.             if (!hasNext() ) throw new NoSuchElementException();
  93.             return A[++index];
  94.            
  95.         }
  96.  
  97.         @Override
  98.         public void remove() {
  99.             throw new UnsupportedOperationException(); 
  100.         }
  101.        
  102.     }
  103.    
  104.     public static void main(String args[]) throws Exception {
  105.         In in = new In("input.txt");
  106.         DynamicArray<String> v = new DynamicArray<String>();
  107.         while (!in.isEmpty()) {
  108.             String s = in.readString();
  109.             v.push_back(s);
  110.         }
  111.         for (String str : v)
  112.             StdOut.print(str); 
  113.     }
  114.  
  115.    
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement