posts - 5, comments - 24, trackbacks - 0, articles - 20
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

ArrayList的用法

Posted on 2006-09-24 22:31 kook 阅读(1942) 评论(1)  编辑  收藏 所属分类: J2SE

他是List接口的实现类。ArrayList类相当于是一个动态数组。

Methods:

1、  void add Object obj

ArrayList的对象里增加一个元素

2、  set (int index, Object  element)

用指定的元素替代此列表中指定位置上的元素。

3、  int size ()

获得ArrayList的对象中元素的个数。

4、  get int index)

返回ArrayList的对象中索引为index的元素。

5、  Object [] toArray ()

ArrayList的对象中的元素返回到一个对象数组中。

PS Arrays.asList(Object[] objs);

    返回一个受指定数组支持的固定大小的列表。

 1 ArrayList al  =   new  ArrayList();
 2
        
 3         al.add( new  Point( 3 , 3
));
 4         al.add( new  Point( 4 , 4
));
 5         al.add( new  Point( 5 , 5
));
 6
        
 7         
/* for(int i=0;i<al.size();i++)
 8
        {
 9
            System.out.println(al.get(i));
10         } */

11         
12
        System.out.println(al);
13         Object[] objs  =
 al.toArray();
14
        System.out.println(Arrays.toString(objs));
15         List L  =
 Arrays.asList(objs);
16

     这里的L是通过Arrays.asList返回一个接口。这时候L的长度就固定不能再变了,不能给L添加元素了。但是可以通过set方法改变L中指定元素的值。

        toArray 方法是将一个List对象转成一个数组,而Arrays.asList方法是将一个数组转成一个List。他们是集合和数组之间的桥梁,有时候方法中的参数可能需要数组或者List的时候,就可以用到他们转换,而不用去重新创建实例。

6、  iterator()

返回一个迭代器。所有继承Collection接口的接口或者这些接口的实现类,都有这个方法。通过List接口对象返回的迭代器没有实现iterator接口中的remove方法。凡是没有实现iterator接口中的remove方法,都会抛出一个 UnsupportedOperationException (不支持的操作)异常。如:

 

1 List l  =   null ;                // List 接口对象l 
2
3 Iterator it  =  l.iterator();   //  通过List接口对象l返回的迭代器it 
4
5 it.next();                    // it 有next方法 
6
7 it.remove();                     //  这里会抛出 UnsupportedOperationException 
8
9


PS :迭代器的作用:

   他可以以一种通用的方式去访问集合中的所有元素。在ArrayList类中可以通过get方法去访问,但是有些集合的实现类中并没有get方法。而我们知道,所有继承Collection接口的接口或者这些接口的实现类,都可以通过iterator()返回一个迭代器,那么我们就可以通过迭代器这种通用的方式去访问集合中的所有元素了。访问方法如下:

 1 ArrayList al  =   new  ArrayList(); 
 2

 3     al.add( new  Point( 3 , 3
)); 
 4

 5     al.add( new  Point( 4 , 4
)); 
 6

 7     al.add( new  Point( 5 , 5
)); 
 8

 9 Iterator it1  =
 al.iterator(); 
10

11         while (it.hasNext())                      //  通过迭代器访问集合元素 

12        
13
              System.out.println(it.next()); 
14        }
 
15


ArrayList 底层采用数组完成,而LinkedList则是以一般的双向链表(double-linked list)完成,其内每个对象除了数据本身外,还有两个引用,分别指向前一个元素和后一个元素。

如果我们经常在List的开始处增加元素,或者在List中进行插入和删除操作,我们应该使用LinkedList,否则的话,使用ArrayList将更加快速。

这两个类都不是同步的,因此他们的效率也比较高。如果要实现同步,可以使用Vector类,Vector类中有一些继承的操作,使用的时候要小心,如果不实现同步一般都不用Vector类。还可以用Collections类的 synchronized 相关方法实现同步,不过效率没有 Vector 类高。


评论

# Seo Services  回复  更多评论   

2009-05-18 06:01 by Seo Services
Excuse me. Assuming either the Left Wing or the Right Wing gained control of the country, it would probably fly around in circles.
I am from Nepal and now study English, tell me right I wrote the following sentence: "If seo process is not constantly going on then the site can be wiped.Create a informational website presence with upto pages."

Thank you so much for your future answers ;-). Anders.

只有注册用户登录后才能发表评论。


网站导航: