Definition of Java study notes List collection, collection traversal, and use of iterators

List collection features: orderly and repeatable; the underlying data structure of ArrayList is sequential storage; the underlying data structure of LinkedList is chain storage;
                

(1) Basic method.
The basic method for query is to add and delete Collection elements, including:
int size(): returns the number of collection elements.
boolean isEmpty(): Returns true if the Collection contains no elements.
boolean Contains(Objecto): Returns true if the Collection contains the specified element.
boolean add(E e): Add an element to the collection, return true if successful, otherwise return false.
boolean remove(Object o): Removes a single instance of the specified element from the Collection.

(2) Batch operation method.
The batch operation method is to operate the entire collection as a unit; including:
boolean addAll(Collection c): Add all elements in the specified collection to the current Collection, and return true if successful.
boolean removeAll(Collection c): Remove all elements, return true if successful.
boolean containsAll(Collection c): Returns true if the Collection contains all elements in the specified Collection.
boolean retainAll(Collection c): Retains elements in the Collection that are also included in the specified Collection.
void clear(): Removes all elements in the Collection.

(3) Array operation method.
The operation of converting a collection into an array includes:
Object[] toArray(): returns an array containing all elements in the Collection.
<T> T[] toArray(T[]a): Returns an array containing all elements in the Collection. The type of the returned array is the same as the runtime type of the specified array.

(4) Iterative operation method.
Iteration operations are methods that provide sequential access to elements for collections. Iterator iterator() returns an object implementing the iterator interface.
The method defined by the iteration interface has boolean hasNext(). As long as there is a next element in the collection, the next element can be obtained with the Object next() method.
 

import java.util.*;

//集合的遍历,迭代器的使用
public class Test{
    public static void main(String[] args){
        Collection<String> myList =new ArrayList<>();  //定义一个存有字符串的集合
        String[] strs={"工藤新一","怪盗基德","鲁邦三世","宫野明美","琴酒","伏特加","天等指天椒"};
        Collections.addAll(myList, strs);               //将数组中的内容复制到集合里面去
        System.out.println("迭代之前:"+myList);          //输出集合中的内容

        Iterator<String> myItertor =myList.iterator();          //定义集合的迭代器

        while(myItertor.hasNext()){
            //通过迭代器获取元素
            String element = myItertor.next();
            //可以进行遍历输出
            System.out.println(element);
            //可以通过迭代器对集合进行操作
            if (element.length()>4){
                System.out.println("“"+element+"”字符长度大于4,删除!");
                myItertor.remove();
            }
        }
        System.out.println("迭代之后:"+myList);          //输出集合中的内容
    }
}

 operation result:

 2. Enhanced for traversal

import java.util.*;
//增强for遍历集合
public class Test{
    public static void main(String[] args){
        Collection<String> myList =new ArrayList<>();  //定义一个存有字符串的集合
        String[] strs={"工藤新一","怪盗基德","鲁邦三世","宫野明美","琴酒","伏特加","天等指天椒"};
        Collections.addAll(myList, strs);               //将数组中的内容复制到集合里面去
         System.out.println("=================增强for遍历====================");
        //采用 for遍历
        for (String s : myList) {
            System.out.println(s);
        }
    }
}

operation result;

 

Guess you like

Origin blog.csdn.net/yvge669/article/details/125830361