Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  4.  * @version 27 SEPTEMBER 2012
  5.  */
  6. public class ArrayList
  7. {
  8.     // instance variables
  9.     public int length;
  10.     public int maxSize;// maxSize default at 100
  11.     public Object [] list;
  12.    
  13.     /**
  14.      * Constructor for objects of class ArrayList
  15.      */
  16.     public ArrayList(){
  17.          this.length = 0;
  18.          maxSize=100;
  19.          list = new Object[maxSize];
  20.          for (int i=0;i<maxSize;i++)
  21.             list[i] = new Object();
  22.     }
  23.    
  24.     public ArrayList(int size)
  25.     {
  26.         this.length = 0;
  27.         maxSize=size;
  28.         list= new Object[maxSize];
  29.         for (int i=0;i<maxSize;i++)
  30.            list[i] = new Object();
  31.     }
  32.    
  33.     /**methods**/
  34.     public boolean isEmpty()
  35.     {
  36.         return (length==0);
  37.     }
  38.    
  39.     public boolean isFull()
  40.     {
  41.         return (length==maxSize);
  42.     }
  43.    
  44.     public void insertAtFront(Object a)
  45.     {
  46.         if (isFull())
  47.         {
  48.             System.err.println("Can't insert in full list.");
  49.         }
  50.         else
  51.         {
  52.             if (! isEmpty())
  53.             {
  54.                 for(int i = length; i > 0; i--)
  55.                     list[i] = list[i - 1];
  56.             }
  57.             //if (isEmpty())
  58.             //    list = new Object[1];
  59.             list [0] = a;
  60.             length++;
  61.         }
  62.     }//end insertAtFront
  63.    
  64.     public void insertAtBack(Object a)
  65.     {
  66.     if (isFull())
  67.         {
  68.             System.err.println("Can't insert in full list.");
  69.         }
  70.         else
  71.         {
  72.             list [length] = a;
  73.             length++;
  74.         }
  75.      }//end insertAtBack
  76.      
  77.     public void insertAt(Object a, int index)//replace element[index] with item
  78.     {
  79.         if (isFull())
  80.             System.err.println("Can't insert in full list.");
  81.         else
  82.             list[index]=a;
  83.     }
  84.      
  85.      public void removeFromFront()
  86.      {
  87.         if (isEmpty())
  88.         {
  89.             System.err.println("Can't remove in empty list.");
  90.         }
  91.         else
  92.         {
  93.             for(int i = 0; i <length; i++)
  94.                 list[i] = list[i+1];
  95.             length--;
  96.         }
  97.     }
  98.    
  99.     public void removeFromBack()
  100.     {
  101.     if (isEmpty())
  102.         {
  103.             System.err.println("Can't remove in empty list.");
  104.         }
  105.         else
  106.         {
  107.             list[length] = null;
  108.             length--;
  109.         }
  110.     }
  111.    
  112.     public void display()
  113.     {
  114.         for(int i = 0; i <length; i++)
  115.         {
  116.             System.out.print(list[i]+" ");
  117.         }
  118.         System.out.println("");
  119.     }
  120. }