09.List Collection

Catalog introduction

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

1.ArrayList

  • 1.1 Simple deduplication
// 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 Collection Features
    • Vector: The underlying data structure is an array, fast query, slow addition and deletion, thread-safe, low efficiency
    • Common method
      • public void addElement(E obj) add element
      • public E elementAt(int index) Get element by index
      • public Enumeration elements() Use similar to iterator, role: used to traverse the Vector collection
    • traverse
Enumeration enumeration = vector.elements() ;
// boolean hasMoreElements(): 判断集合中是否存在下一个元素
// E nextElement(): 获取下一个元素
while(enumeration.hasMoreElements()) {
    System.out.println(enumeration.nextElement());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025757&siteId=291194637