ArrayList使用

ArrayList

ArrayList继承了AbstractList,实现了List,Serializable等接口。它内部实现是是基于数组的。

例子

ackage com.fang.java.javabase.firstconnection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/*

// Collection中定义的API
boolean             add(E object)
boolean             addAll(Collection<? extends E> collection)
void                clear()
boolean             contains(Object object)
boolean             containsAll(Collection<?> collection)
boolean             equals(Object object)
int                 hashCode()
boolean             isEmpty()
Iterator<E>         iterator()
boolean             remove(Object object)
boolean             removeAll(Collection<?> collection)
boolean             retainAll(Collection<?> collection)
int                 size()
<T> T[]             toArray(T[] array)
Object[]            toArray()
// AbstractCollection中定义的API
void                add(int location, E object)
boolean             addAll(int location, Collection<? extends E> collection)
E                   get(int location)
int                 indexOf(Object object)
int                 lastIndexOf(Object object)
ListIterator<E>     listIterator(int location)
ListIterator<E>     listIterator()
E                   remove(int location)
E                   set(int location, E object)
List<E>             subList(int start, int end)
// ArrayList新增的API
Object               clone()
void                 ensureCapacity(int minimumCapacity)
void                 trimToSize()
void                 removeRange(int fromIndex, int toIndex)
*/
@Slf4j
public class ArrayListTest {
    public static void main(String[] args) {
        ArrayList<Pet> list=new ArrayList<Pet>();
  
        //add all
        list.addAll(Pets.getPets());
        log.info("list size{}",list.size());
        //contains
        log.info("contain obj");
        for(Pet pet:list){
            if(list.contains(pet)){
                log.info("{}:{}",pet.toString(),list.indexOf(pet));
            }
        }
        //remove

        // iterator
        log.info("iterator list");
        Iterator<Pet> it=list.iterator();
        while(it.hasNext()){
            log.info(it.next().toString());
        }
        //foreach
        log.info("Foreach list");
        for(Pet pet:list){
            log.info(pet.toString());
        }
        //to Array
        Pet[]pets=list.toArray(new Pet[0]);
        log.info("ToArray");
        for(Pet pet:pets){
            log.info(pet.toString());
        }
        //Array to List
        List<Pet> list2=Arrays.asList(pets);
        log.info("array to list");
        for(Pet pet:list2){
            log.info(pet.toString());
        }
        //contain
        log.info("contains object");
        for(Pet pet:list){
            if(list2.contains(pet)){
                log.info("{}:{}",pet.toString(),list2.indexOf(pet));
            }
        }
        //contain all
        log.info("contain all");
        if(list2.containsAll(list)){
            log.info("list2 contain list");
        }
        //remove
        //remove all
        log.info("Removeall list");
        list.removeAll(list);
        for(Pet pet:list){
            log.info(pet.toString());
        }
    }
}
上一篇:mysql -在终端操作数据库(不定时update)


下一篇:猫狗队列