JAVA容器ArrayList使用

ArrayList

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

例子

package 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 collection)
void                clear()
boolean             contains(Object object)
boolean             containsAll(Collection collection)
boolean             equals(Object object)
int                 hashCode()
boolean             isEmpty()
Iterator         iterator()
boolean             remove(Object object)
boolean             removeAll(Collection collection)
boolean             retainAll(Collection collection)
int                 size()
 T[]             toArray(T[] array)
Object[]            toArray()
// AbstractCollection中定义的API
void                add(int location, E object)
boolean             addAll(int location, Collection collection)
E                   get(int location)
int                 indexOf(Object object)
int                 lastIndexOf(Object object)
ListIterator     listIterator(int location)
ListIterator     listIterator()
E                   remove(int location)
E                   set(int location, E object)
List             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 list=new ArrayList();
  
        //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 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 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());
        }
    }
}

你可能感兴趣的:(JAVA容器ArrayList使用)