Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * Write a description of class OrderedArrayList here.
  4.  *
  5.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  6.  * @version 11/11/2012
  7.  */
  8. public class OrderedArrayList extends ArrayList
  9. {
  10.     //default constructor
  11.     public OrderedArrayList()
  12.     {
  13.         super();
  14.     }
  15.     //normal constructor
  16.     public OrderedArrayList(int size)
  17.     {
  18.         super(size);
  19.     }
  20.    
  21.     public int seqSearch(Object item)
  22.     {
  23.         /*If the item is found, returns the location in the array where item is found
  24.          * otherwise return -1;
  25.            */
  26.         int a = -1;
  27.         for (int i=0;i<list.length;i++)
  28.         {
  29.             //System.out.println("Element:"+list[i]+" | Length:"+length);
  30.             //if (list[i].toString().equals(item.toString()))
  31.             if (list[i].equals(item))
  32.             {  
  33.                 a = i;
  34.                 break;
  35.             }
  36.         }
  37.         return a;
  38.     }
  39.    
  40.     public int binarySearch(Object item)
  41.     {
  42.         /*If the item is found, returns the location in the array where item is found
  43.          * otherwise return -1;
  44.            */
  45.         int first = 0;
  46.         int last = (length-1);
  47.         int mid = -1;
  48.         boolean found = false;
  49.         while (first <= last && !found)
  50.         //loop until end list or finally found
  51.         {
  52.             mid = (first+last)/2;
  53.             if (list[mid]==item)
  54.                 found = true;
  55.             else
  56.                 if ((Integer)item < (Integer) list[mid] )//item<list[mid]
  57.                     first = mid+1;
  58.                 else
  59.                     last = mid+1;
  60.         }
  61.         if (found)
  62.             return mid;
  63.         else
  64.             return -1;
  65.     }
  66.    
  67.     public void insert(Object item)
  68.     {
  69.         /*
  70.         * method to insert item at the end of the list
  71.         * however, first the list is searched to
  72.         * see if the item to be inserted is already in the list.
  73.         * postcondition: item is inserted and length++.
  74.         * if insertItem is already in the list or the list is full,
  75.         *     an appopriate message is displayed.
  76.         */
  77.         if (isFull())//check for full list
  78.             System.err.println("Can't insert ["+item.toString()+"] in full list.");
  79.         else if(isEmpty())//if empty, can instantly insert into list.
  80.         {
  81.             list[length] = item;
  82.             ++length;
  83.             //insertAtBack(item);//using ArrayList method, but remember the length already added in
  84.         }
  85.         //else if (binarySearch(item) != -1) //check if already exist
  86.         else if (seqSearch(item)!=-1)
  87.             System.err.println("["+item.toString()+"]Already in list.");
  88.         else if ((Integer)item>=(Integer)list[length-1])//Input instantly if it is larger than last element.
  89.         {    
  90.             list[length++] = item;
  91.             //insertAtBack(item);//although this is slower
  92.         }
  93.         else
  94.         {
  95.            //because the previous statements false
  96.            //this means the current item is smaller than last element
  97.            //store this item before looping
  98.            int current = length - 1;
  99.            list[current+1] = list[current];
  100.            list[current] = item;
  101.            length++;
  102.            //as it already been input into list, length of list increased
  103.            //so, we only need to put it in its position
  104.            while (current!=0)
  105.            {
  106.                //--current;
  107.                if ((Integer)list[--current] > (Integer) item){
  108.                    
  109.                     list[current + 1] = list[current];//move the checked element up
  110.                     list[current] = item;//my ArrayList class don't have insertAt() method before this.
  111.                     //and using method insertAt() cause error that not supposed to happened.
  112.                }
  113.                else
  114.                     break;//since already reach element smaller than current item, break
  115.            }
  116.         }
  117.  
  118.     }
  119. }