vector

  vector类是可以实现可增长的对象数组,它使用capacity和capacityIncrement来管理大小,capacity表示向量实际的存储大小,capacity要大于等于向量的大小,否则capacity将以capacityIncrement增加,其目的是为了减少重分配的量。

  例如下面有一个Attribute类,目的是为了保存一个键/值对:

public class Attribute implements Cloneable{
        private String name;
        private String Value;
        private char delim;  
        
        public Attribute(String name, String value, char delim)
        {
                  this.name = name;
                  this.value = value;
                  this.dlim = delim;
        }

        public Attribute()
        {
                  this("","",(char)0);
        }

        public Attribute(String name, String value)
        {
               this(name,value,(char)0);
        }

        public String getName()
        {
               return name;
        }

        public String getValue()
        {
               return value;
        }

        public char getDelim()
        {
               return delim;
        }

        public void setName(String name)
        {
               this.name = name;
        }

        public void setValue(String value)
        {
              this.value = value;
        }

        public void setDelim(char ch)
        {
             this.delim = ch
        }

        public Object clone()
        {
             return new Attribute(name,value,delim);
        }
}

  AttributeList类中有一个属性是Vector类,用力管理所个Attribute实例:

public class AttributeList implements Cloneable{
       protected Vector vec;
    
       public AttributeList()
       {
                vec = new Vector();
       }


       synchronized public Attribute get(int id)
       {
                if(id < vec.size())
                          return (Attribute)vec.elementAt(id);
                else
                          return null;
        }

        synchronized public Attribute get(String id)
        {
                int i=0;
                while(get(i)!=null)
                {
                          if(get(i).getName().equalsIgnoreCase(id))
                                    return get(i);
                          i++;
                }

                return null;
        }

        synchronized public void add(Attribute a)
        {
                vec.addElement(a);
        }

         synchronized public void clear()
         {
                vec.removeAllElements();
         }

         synchronized public boolean isEmpty()
         {
                return (vec.size()<=0)
         }

         synchronized public int length()
         {
                  return vec.size();
          }

         synchronized public void set(String name, String value)
         {
                  if(name == null)
                         return;
                  if(value == null)
                         value = "";
          
                  Attribute a = get(name)
                  if(a == null)
                  {
                        a = new Attribute(name, value);
                        add(a);
                  }else{
                        a.setValue(value);
                  }
         }

          public Object clone()
          {
                  int i;
                  AttributeList rtn = new AttributeList();
                  for(int i=0; i<vec.size(); i++)
                        rtn.add((Attribute)get(i).clone());
                  return rtn;
          }
}

 

posted on 2017-06-29 19:41  残余的光  阅读(167)  评论(0编辑  收藏  举报

导航