当前位置:编程学习 > JAVA >>

java源码分析之ArrayList

 ArrayList就是传说中的动态数组,就是Array的复杂版本,它提供了如下一些好处:动态的增加和减少元素、灵活的设置数组的大小......
 
    认真阅读本文,我相信一定会对你有帮助。比如为什么ArrayList里面提供了一个受保护的removeRange方法?提供了其他没有被调用过的私有方法?
 
    首先看到对ArrayList的定义:
 
[java]  
public class ArrayList<E> extends AbstractList<E>  implements List<E>, RandomAccess, Cloneable, java.io.Serializable  
 从ArrayList<E>可以看出它是支持泛型的,它继承自AbstractList,实现了List、RandomAccess、Cloneable、java.io.Serializable接口。
 
    AbstractList提供了List接口的默认实现(个别方法为抽象方法)。
 
    List接口定义了列表必须实现的方法。
 
    RandomAccess是一个标记接口,接口内没有定义任何内容。
 
    实现了Cloneable接口的类,可以调用Object.clone方法返回该对象的浅拷贝。
 
    通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。序列化接口没有方法或字段,仅用于标识可序列化的语义。
 
    ArrayList的属性
 
    ArrayList定义只定义类两个私有属性:
 
[java]  
/** 
      * The array buffer into which the elements of the ArrayList are stored. 
      * The capacity of the ArrayList is the length of this array buffer. 
      */  
     private transient Object[] elementData;  
   
     /** 
      * The size of the ArrayList (the number of elements it contains). 
      * 
      * @serial 
      */  
     private int size;  
 
[java]  
很容易理解,elementData存储ArrayList内的元素,size表示它包含的元素的数量。  
  
  有个关键字需要解释:transient。  
  
  Java的serialization提供了一种持久化对象实例的机制。当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient。  
ansient是Java语言的关键字,用来表示一个域不是该对象串行化的一部分。当一个对象被串行化的时候,transient型变量的值不包括在串行化的表示中,然而非transient型的变量是被包括进去的。  
  
  有点抽象,看个例子应该能明白。  
 
[java] 
public class UserInfo implements Serializable {  
     private static final long serialVersionUID = 996890129747019948L;  
     private String name;  
     private transient String psw;  
   
     public UserInfo(String name, String psw) {  
         this.name = name;  
         this.psw = psw;  
     }  
   
     public String toString() {  
         return "name=" + name + ", psw=" + psw;  
     }  
 }  
   
 public class TestTransient {  
     public static void main(String[] args) {  
         UserInfo userInfo = new UserInfo("张三", "123456");  
         System.out.println(userInfo);  
         try {  
             // 序列化,被设置为transient的属性没有被序列化  
             ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(  
                     "UserInfo.out"));  
             o.writeObject(userInfo);  
             o.close();  
         } catch (Exception e) {  
             // TODO: handle exception  
             e.printStackTrace();  
         }  
         try {  
             // 重新读取内容  
             ObjectInputStream in = new ObjectInputStream(new FileInputStream(  
                     "UserInfo.out"));  
             UserInfo readUserInfo = (UserInfo) in.readObject();  
             //读取后psw的内容为null  
             System.out.println(readUserInfo.toString());  
         } catch (Exception e) {  
             // TODO: handle exception  
             e.printStackTrace();  
         }  
     }  
 }  
 被标记为transient的属性在对象被序列化的时候不会被保存。
 
    接着回到ArrayList的分析中......
 
    ArrayList的构造方法
 
    看完属性看构造方法。ArrayList提供了三个构造方法:
 
[java]  
/** 
      * Constructs an empty list with the specified initial capacity. 
      */  
     public ArrayList(int initialCapacity) {  
     super();  
         if (initialCapacity < 0)  
             throw new IllegalArgumentException("Illegal Capacity: "+  
                                                initialCapacity);  
     this.elementData = new Object[initialCapacity];  
     }  
   
     /** 
      * Constructs an empty list with an initial capacity of ten. 
      */  
     public ArrayList() {  
     this(10);  
     }  
   
     /** 
      * Constructs a list containing the elements of the specified 
      * collection, in the order they are returned by the collectio
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,