Saturday, October 1, 2011

Different about List between Add Method and Remove Method

C# List Note

http://allantech.blogspot.com/2007/03/performance-arraylist-vs-list.html


Array :  array element can be  of  any type,including an array type , an array with n elements and
            the "n" must be given a value.


List: List element can be a strongly typed list of objects that can be accessed by index.List with
       elements can be any Length.


ArrayList : ArrayList element can be of any type,ArrayList with n elements can be any Length.







--------------------------------------------------------------------------------------------------

Java arrayList Note

get(int) : Effective.
add(Object obj) : Effective.
set(int index,Object obj):Effective.

add(int index,Object obj):Low Effective.
remove(int index): Low Effective.
contains(Object obj):Low Effective.(Because Of Calling index Of)

Remove List Way:
1.Use Remove method,Delete Element.
2.Rebird new ArrayList,Insert Useless Element.

--------------------------------------------------------------------------------------------------
Java Example
There is an ArrayList include Integer,We need to Delete odd number



public class Example
{
    private static List exampleNew(int cnt)
    {
       List p = new ArrayList();
       for(int i =0; i < cnt;i++)
         p.Add(new Integer(i));

      List o = new ArrayList(100);
      for(int i = 0;i < p.Size();i++)
          if(i%2==0)
              o.Add(p.get(i));

     return o;
    }

    private static List exampleRemove(int cnt)
    {
         List p = new List();
         for(int i =0; i < cnt ; i++)
             p.Add(new Integer(i));

        for(int i = 0 ; i < p.Size();i++)
            if(i%2 != 0 )
                p.Remove(i);
           
        return p;
    }

     public static void example(int total,int cnt)
    {
         double time01,time02;
            time01 = System.currentTimeMillis();
            for(int i =0; i < total / cnt; i ++)
                exampleNew(cnt);
           time02 = System.currentTimeMillis();
         
           double time03 = time02-time01;
           time01 = System.currentTimeMillis();
           for(int i =0; i < total/cnt;i++)
                 exampleRemove(cnt);
           time02 = System.currentTimeMillis();

           double time04 = time02-time01

           System.out.println("List Size : " + cnt);
           System.out.println("using New List:" + time03+"ms");
           System.out.println("using Remove:"+time04+"ms");
           System.out.println("New ---- Remove" + time03/time04+ " :1");
    }



     public static void main(String[] args)
    {
         double time01,time02;
          int total = 100*1000;
          int cnt=1;
          while(cnt <= total)
          {
             example(total,cnt);
             cnt *=10;
          }
    }

 
}

--------------------------------------------------------------------------------------------------
Result:
      When ArrayList is equal or bigger then 1000,the effective will have big different,
therefore, Use Add Metod is greater then Remove Method.







No comments:

Post a Comment