ArrayList


This example of Array List algorithm look like....

public class ArrayList{
    private int length;
    private int maxSize;
 
    private Object []list;
 
    public ArrayList(){
        length=0;
        maxSize=10; //defaut initialize
     
        list=new Object[maxSize];
    }
 
    public ArrayList(int size){
        length=0;
        maxSize=size;
     
        list=new Object[maxSize];
    }
 
    public Object get(int index){
        return list[index];
    }
 
    public boolean isEmpty()
    {
        return (length == 0);
    }

    public boolean isFull()
    {
        return (length == maxSize);
    }
    public void add(Object element)
    {
        if(isFull()) //list is full
            System.err.println("Cannot insert in a full list");
        else
        {
            for(int i = length; i>0; i--){
                list[i]=list[i-1];//item at the specified position
             
            }
            list[0]=element;
            length++;      //increment the length
        }
    }//end add
 
    public void add(int loc,Object element)
    {
        if(isFull()) //list is full
            System.err.println("Cannot insert in a full list");
        else
        {
            for(int i = length; i>loc; i--){
                list[i]=list[i-1];//item at the specified position
             
            }
            list[loc]=element;
            length++;      //increment the length
        }
    }//end add
 
    public void remove(int loc)
    {
        if(isEmpty()) //list is empty
            System.err.println("List is empty");
        else
        {
            length--;
            for(int i=loc; i<length; i++){
                list[i]=list[i+1]; //item at the specified position
            }
        }
    }//end remove
 
    public void display(){
        if(isEmpty()){
            System.out.println("(Empty)");
        }
        else{
            System.out.printf("[0] = %2d",list[0]);
            for(int i=1;i<length;i++){
                System.out.printf(" | ["+i+"] = %2d",list[i]);
            }
            System.out.println();
        }   
    }
 
    public int size()
    {
        return length;
    }

    public int maxSize()
    {
        return maxSize;
    }  
}


Now, try use this method in main program and see what the result is...


public class mainProgram{
        public static void main(String []arg){
        ArrayList array=new ArrayList();

        int rand=0;
        int num=0;
        for(int i=0;i<10;i++){
            rand=(int)(Math.round(Math.random()*2));
            num=(int)(Math.random()*99);
            if(rand==0||array.size()==0){
                System.out.println("Add ");
                array.add(num);
            }
            else if(rand==1&&array.size()>0){
                rand=(int)(Math.round(Math.random()*(array.size()-1)));
                System.out.println("Add at "+rand);
                array.add(rand,num);
            }
            else if(rand==2&&array.size()>0){
                rand=(int)(Math.round(Math.random()*(array.size()-1)));
                System.out.println("Remove at "+rand);
                array.remove(rand);
            }
            array.display();
            System.out.println();
        }
        System.exit(0);
    }
}

Sample Running :



































p/s : If you any question please comment me or email me...tq
                                                                                                                                                      Created By : Z-man, 2012

1 comment: