add增加
第一种是直接add对象,把对象加在最后面heros.add(new Hero(“hero ” + i))
第二种是在指定位置加对象 heros.add(3, specialHero);
contains 判断一个对象是否在容器中
判断标准: 是否是同一个对象,而不是name是否相同
import java.util.ArrayList;
import charactor.Hero;
public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList();
// 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero(“hero ” + i));
}
Hero specialHero = new Hero(“special hero”);
heros.add(specialHero);
System.out.println(heros);
// 判断一个对象是否在容器中
// 判断标准: 是否是同一个对象,而不是name是否相同
System.out.print(“虽然一个新的对象名字也叫 hero 1,但是contains的返回是:”);
System.out.println(heros.contains(new Hero(“hero 1”)));
System.out.print(“而对specialHero的判断,contains的返回是:”);
System.out.println(heros.contains(specialHero));
}
}
get获取指定位置的对象,如果输入的下标越界,一样会报错
import java.util.ArrayList;
import charactor.Hero;
public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList();
// 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero(“hero ” + i));
}
Hero specialHero = new Hero(“special hero”);
heros.add(specialHero);
//获取指定位置的对象
System.out.println(heros.get(5));
//如果超出了范围,依然会报错
System.out.println(heros.get(6));
}
}
indexOf用于判断一个对象在ArrayList中所处的位置
与contains一样,判断标准是对象是否相同,而非对象的name值是否相等.
import java.util.ArrayList;
import charactor.Hero;
public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList();
// 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero(“hero ” + i));
}
Hero specialHero = new Hero(“special hero”);
heros.add(specialHero);
System.out.println(heros);
System.out.println(“specialHero所处的位置:”+heros.indexOf(specialHero));
System.out.println(“新的英雄,但是名字是\”hero 1\”所处的位置:”+heros.indexOf(new Hero(“hero 1”)));
}
}
remove用于把对象从ArrayList中删除,
remove可以根据下标删除ArrayList的元素 remove(int index)
remove可以删除ArrayList中的特定对象的元素 remove(object o)
import java.util.ArrayList;
import charactor.Hero;
public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList();
// 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
Hero specialHero = new Hero("special hero");
heros.add(specialHero);
System.out.println(heros);
heros.remove(2);
System.out.println("删除下标是2的对象");
System.out.println(heros);
System.out.println("删除special hero");
heros.remove(specialHero);
System.out.println(heros);
}
}
toArray可以把一个ArrayList对象转换为数组。
需要注意的是,如果要转换为一个Hero数组,那么需要传递一个Hero数组类型的对象给toArray()作为输入参数,这样toArray方法才知道,你希望处理Arraylist中的哪种类型的对象。默认的返回值类型为Object,因此这里还要强制转化一下类型
import java.util.ArrayList;
import charactor.Hero;
public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList();
// 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero(“hero ” + i));
}
Hero specialHero = new Hero(“special hero”);
heros.add(specialHero);
System.out.println(heros);
Hero hs[] = (Hero[])heros.toArray(new Hero[]{});
System.out.println(“数组:” +hs);
}
}
addAll 把另一个容器所有对象都加进来
import java.util.ArrayList;
import charactor.Hero;
public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList();
// 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero(“hero ” + i));
}
System.out.println(“ArrayList heros:\t” + heros);
//把另一个容器里所有的元素,都加入到该容器里来
ArrayList anotherHeros = new ArrayList();
anotherHeros.add(new Hero("hero a"));
anotherHeros.add(new Hero("hero b"));
anotherHeros.add(new Hero("hero c"));
System.out.println("anotherHeros heros:\t" + anotherHeros);
heros.addAll(anotherHeros);
System.out.println("把另一个ArrayList的元素都加入到当前ArrayList:");
System.out.println("ArrayList heros:\t" + heros);
}
}
ArrayList实现了接口List
因为ArrayList实现了List接口,所以List接口的方法ArrayList都实现了
上述的方法,List都可以实现