09.List集合

目录介绍

  • 1.ArrayList
  • 2.Vector
  • 3.LinkedList

1.ArrayList

  • 1.1 简单去重
// ArrayList去除集合中字符串的重复值(字符串的内容相同)
// 1. 定义老的集合对象
ArrayList oldList = new ArrayList() ;
// 2. 添加元素
oldList.add("刘亦菲") ;
oldList.add("朱茵") ;
oldList.add("李冰冰 ") ;
oldList.add("范冰冰") ;
oldList.add("李冰冰 ") ;
// 3. 创建新的集合对象
ArrayList newList = new ArrayList() ;
// 4. 遍历老集合对象
for(int x = 0 ; x < oldList.size() ; x++) {
    // 获取当前遍历的元素
    Object object = oldList.get(x) ;
    // 判断新集合中是否包含当前遍历的元素
    if(!newList.contains(object)) {
        newList.add(object) ;
    }
}

2.Vector

  • 2.1 Vectot集合特点
    • Vector: 底层的数据结构是数组, 查询快 , 增删慢 , 线程安全的 , 效率低
    • 常用方法
      • public void addElement(E obj) 添加元素
      • public E elementAt(int index) 根据索引获取元素
      • public Enumeration elements() 使用类似于迭代器 , 作用: 用来遍历Vector集合
    • 遍历
Enumeration enumeration = vector.elements() ;
// boolean hasMoreElements(): 判断集合中是否存在下一个元素
// E nextElement(): 获取下一个元素
while(enumeration.hasMoreElements()) {
    System.out.println(enumeration.nextElement());
}

猜你喜欢

转载自my.oschina.net/zbj1618/blog/1803194