ArrayList
ArrayList继承了AbstractList,实现了List,Serializable等接口。它内部实现是是基于数组的。
List的实现类的clone都是浅复制:如果只是remove,就不会影响到浅复制的应用,修改会影响
add的时候当前长度大于数组长度就会扩容数组,把当前的数组内容拷贝到新数组然后重新 引用。
ArrayList可以转为数组,最好的方法是就是ublic
例子
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pet{
private String name;
}
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Data
public class Pets{
public static List<Pet> getPets(){
List<Pet> list=new ArrayList<Pet>();
Pet petdog=new Pet("dog");
Pet petpig=new Pet("pig");
Pet petxiaoji=new Pet("xiaoji");
list.add(petdog);
list.add(petpig);
list.add(petxiaoji);
return list;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
@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");
}
//clone 浅复制
ArrayList<Pet> list3=(ArrayList)list.clone();
log.info(list3.toString());
list.get(2).setName("reset xiaoji");
log.info(list3.toString());
//remove
//remove all
log.info("Removeall list");
list.removeAll(list);
list.clear();
for(Pet pet:list){
log.info(pet.toString());
}
输出
2019-07-11 21:23:28,321 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - list size3
2019-07-11 21:23:28,326 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - contain obj
2019-07-11 21:23:28,327 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=dog):0
2019-07-11 21:23:28,327 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=pig):1
2019-07-11 21:23:28,327 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=xiaoji):2
2019-07-11 21:23:28,327 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - iterator list
2019-07-11 21:23:28,327 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=dog)
2019-07-11 21:23:28,327 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=pig)
2019-07-11 21:23:28,327 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=xiaoji)
2019-07-11 21:23:28,327 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Foreach list
2019-07-11 21:23:28,328 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=dog)
2019-07-11 21:23:28,328 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=pig)
2019-07-11 21:23:28,328 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=xiaoji)
2019-07-11 21:23:28,328 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - ToArray
2019-07-11 21:23:28,328 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=dog)
2019-07-11 21:23:28,328 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=pig)
2019-07-11 21:23:28,328 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=xiaoji)
2019-07-11 21:23:28,328 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - array to list
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=dog)
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=pig)
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=xiaoji)
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - contains object
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=dog):0
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=pig):1
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Pet(name=xiaoji):2
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - contain all
2019-07-11 21:23:28,329 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - list2 contain list
2019-07-11 21:23:28,330 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - [Pet(name=dog), Pet(name=pig), Pet(name=xiaoji)]
2019-07-11 21:23:28,330 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - [Pet(name=dog), Pet(name=pig), Pet(name=reset xiaoji)]
2019-07-11 21:23:28,330 [main] INFO com.fang.java.javabase.firstcollection.ArrayListTest - Removeall list