document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.     Konsep arraylist ini adalah untuk memudahkan menyimpan data tanpa perlu risau akan size array tersebut.
  3.     Ianya dinamic array di mana jika size sudah mencapai limitnya maka ianya akan membuat array baru dengan size
  4.     yang lebih besar supaya boleh menyimpan sebanyak data yang user mahukan.
  5. */
  6.  
  7.  
  8. public class ArrayList{
  9.     private int length;
  10.     private int maxSize;
  11.  
  12.     private Object []list;
  13.  
  14.     public ArrayList(){
  15.         length=0;
  16.         maxSize=10; //defaut initialize
  17.      
  18.         list=new Object[maxSize];
  19.     }
  20.  
  21.     public ArrayList(int size){
  22.         length=0;
  23.         maxSize=size;
  24.      
  25.         list=new Object[maxSize];
  26.     }
  27.  
  28.     public Object get(int index){
  29.         return list[index];
  30.     }
  31.  
  32.     public boolean isEmpty()
  33.     {
  34.         return (length == 0);
  35.     }
  36.  
  37.     public boolean isFull()
  38.     {
  39.         return (length == maxSize);
  40.     }
  41.     public void add(Object element)
  42.     {
  43.         if(isFull()) {// if list is full
  44.             maxSize *= 2; //maxSize will be multiplied by 2
  45.             Object [] newlist = new Object[maxSize]; //create new array with new capacity(maxSize)
  46.             System.arraycopy(this.list, 0, newlist, 0, this.length); //copy old array to new array (list -> newlist)
  47.             this.list = newlist;
  48.         }
  49.         for(int i = length; i>0; i--){
  50.             list[i]=list[i-1];//item at the specified position  
  51.         }
  52.         list[0]=element;
  53.         length++;      //increment the length
  54.     }//end add
  55.  
  56.     public void add(int loc,Object element)
  57.     {
  58.         if(isFull()) {// if list is full
  59.             maxSize *= 2; //maxSize will be multiplied by 2
  60.             Object [] newlist = new Object[maxSize]; //create new array with new capacity(maxSize)
  61.             System.arraycopy(this.list, 0, newlist, 0, this.length); //copy old array to new array (list -> newlist)
  62.             this.list = newlist;
  63.         }
  64.         for(int i = length; i>loc; i--){
  65.             list[i]=list[i-1];//item at the specified position      
  66.         }
  67.         list[loc]=element;
  68.         length++;      //increment the length
  69.     }//end add
  70.  
  71.     public void remove(int loc)
  72.     {
  73.         if(isEmpty()) //list is empty
  74.             System.err.println("List is empty");
  75.         else
  76.         {
  77.             length--;
  78.             for(int i=loc; i<length; i++){
  79.                 list[i]=list[i+1]; //item at the specified position
  80.             }
  81.         }
  82.     }//end remove
  83.  
  84.     public void display(){
  85.         if(isEmpty()){
  86.             System.out.println("(Empty)");
  87.         }
  88.         else{
  89.             System.out.printf("[0] = %2d",list[0]);
  90.             for(int i=1;i<length;i++){
  91.                 System.out.printf(" | ["+i+"] = %2d",list[i]);
  92.             }
  93.             System.out.println();
  94.         }    
  95.     }
  96.  
  97.     public int size()
  98.     {
  99.         return length;
  100.     }
  101.  
  102.     public int maxSize()
  103.     {
  104.         return maxSize;
  105.     }  
  106. }
');