Collection是所有集合的接口
都是java.util.*包下的
List是接口 (ArrayList,LinkedList)是它的重要实现类
List和数组相似,有序并且长度可以变化
泛型List<PhoneType> 变量list全部存储的都是PhoneType的对象
可以添加泛型子类型的对象实例
package Y1_28;
import java.util.*;
public class ListTest {
public static void main(String[] args) {
ListTest lt = new ListTest();
PhoneType pt1 = new PhoneType("OPPO",6373392);
PhoneType pt2 = new PhoneType("三星",6060950);
PhoneType pt3 = new PhoneType("华为",5904723);
PhoneType pt4 = new PhoneType("苹果",5826622);
PhoneType[] arrPt = {pt3,pt4};
List<PhoneType> list = new ArrayList<PhoneType>();
//add()添加
list.add(new PhoneType("vivo",8330510));
//list.add(new Integer(1)); 泛型起了作用,如果没加则不会报错
//可以选择位置存储,不能越界
list.add(1, pt1);
//查!!!!!!!asList
list.addAll(Arrays.asList(arrPt));
//显示手机信息
lt.showPhone(list);
System.out.println("-----------------------");
//修改list中存储的元素get();
System.out.println("将苹果修改为英文");
list.set(3, new PhoneType("Apple",5826622));
System.out.println("修改过后的手机型号:"+list.get(3).getLogo());
System.out.println("修改成功!");
//删除一个元素remove();
System.out.println("去掉三星手机");
list.remove(pt2); //list.remove(2);同样效果
System.out.println("-----------------------");
lt.showPhone(list);
System.out.println("成功去除");
}
public void showPhone(List list){
//用Iterate迭代遍历,此时也应该用泛型
Iterator<PhoneType> it = list.iterator();
while(it.hasNext())
{
PhoneType temp = it.next();
System.out.println("手机产销排行:"+temp.getLogo()+" 热度为"+temp.getPopular());
}
}
}
class PhoneType{
private String logo;
private long popular;
PhoneType(String logo,long popular)
{
this.logo = logo;
this.popular = popular;
}
public long getPopular() {
return popular;
}
public void setPopular(long popular) {
this.popular = popular;
}
public String getLogo() {
return logo;
}
}