美文网首页
No.3.1 ArrayList类/Vector类

No.3.1 ArrayList类/Vector类

作者: 醒着的码者 | 来源:发表于2016-04-10 22:21 被阅读45次

ArrayList类/Vector类:

1.两者底层都是数组结构;
2.ArrayList:是线程不安全的,对他的操作并不同步;
3.Vector:线程同步的;
4.在效率方面ArrayList不用同步,所以使用效率要高很多;


ArrayList 类:

  • ArrayList提供了Collection的两个构造函数;
Array提供了使用角标操作集合的父类增删改查的方法:

  • (1)void add(int index, E element),注意这里没有了返回值;
    (2)boolean addAll(int index, Collection<? extends E> c);注意这里返回是布尔值;
    这里的index类似于数组中的index,从0开始,插入的元素占据集合中的index角标位置;

  • (1)E remove(int index);这里返回的是被删除角标位置的元素;
    (2)void removeRange(int fromIndex, int toIndex);删除from-to角标的元素。没有返回值

  • (1)public void trimToSize();修改ArrayList的当前的大小,使之等于当前集合内容的大小,我们可以用它来节省内存空间。
    (2)public E set(int index,E element);设置某个角标的值;返回的是这个角标原来的元素;注意这里不能设置超越size大小的角标值;
    (3)ensureCapacity(int minCapacity);设置集合的最小值
    -查
    (1)根据角标查:public E get(int index);返回index角标的元素值
    (2)根据对象查: public int indexOf(Object o);返回该元素的角标;
  • 其他方法
    public List<E> subList (int fromIndex, int toIndex);返回一个新集合;该集合中的内容为对应集合的子集;

ArrayListDemo:

public class ArrayListDemo {

public static void main(String[] args) {
    addTest();
}
public static void addTest(){
    
    ArrayList<String> list = new ArrayList<String>(10);
    //collection的添加方法:
    list.add("a");
    list.add("a");
    list.add("b");
    list.add("c");
    list.add("d");
    //List的插入添加方法;
    list.add(0, "插入");//插入 a a b c d 
    
    List<String> list2 = new ArrayList<String>();
    list2.add("44");
    list2.add("55");
    list2.add("66");
    //在某个角标出添加一个集合;
    list.addAll(2, list2);//a 44 55 66 a b c d
    System.out.println(list.remove("a"));//true
    System.out.println(list.remove(7));//d
    System.out.println(list.set(6, "我修改了"));//c
    list.ensureCapacity(20);
    System.out.println(list.size());//虽然修改了集合的最小值但是仍没有
    for(Iterator<String> it = list.iterator(); it.hasNext();){
        System.out.print(it.next() + " ");//插入 44 55 66 a b 我修改了 
    }
    List<String> newlist = list.subList(2, 5);
    for(Iterator<String> it = newlist.iterator(); it.hasNext();){
        System.out.print(it.next() + " ");//55 66 a 
    }
    
  }

}

Vector类:

  • 与ArrayList类的区别:
    1 Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的同步必然要影响性能,因此,ArrayList的性能比Vector好。
    2 当Vector或ArrayList中的元素超过它的初始大小时,Vector会将它的容量翻倍,而ArrayList只增加50%的大小,这样,ArrayList就有利于节约内存空间。

  • 方法的区别:
    (1)与ArrayList相比Vector多了*element方法:如 void addElement(E obj);用于增加一个元素在集合尾部;
    (2)int capacity()返回Vector集合的大小;这里的大小是空间大小,而不是元素的“个数”;
    (3)多了一种迭代方法:

    public static void VectorTest(){
      Vector<String> v = new Vector<>(3);
      v.addElement("a");
      v.addElement("b");
      v.addElement("c");
      v.addElement("c");
      System.out.println(v.capacity());//6增加了一倍
      Enumeration<String> en = v.elements();
      while(en.hasMoreElements()){
          System.out.println(en.nextElement());//a b c c
      }
    }
    

也可用Iterator迭代方法;注意这两个迭代方法都是线程安全的。

相关文章

网友评论

      本文标题:No.3.1 ArrayList类/Vector类

      本文链接:https://www.haomeiwen.com/subject/chymlttx.html