T和E可以是任何类型
class People{ T age;//String E name;//int public T getAge() { return age; } public void setAge(T age) { this.age = age; } public E getName() { return name; } public void setName(E name) { this.name = name; } }
进行调用:
下面两种都行
PeopleList Arraylistp=new People(); People p=new People();
ArrayListset:a=new ArrayList(); //等效方式:List list =new ArrayList(); a.add("a"); a.add("a"); a.add("a"); a.add("b");//向集合里增加元素 List list =new CopyOnWriteArrayList();//写时复制 a.set(2,"mama");//将集合里第二个元素替换为"mama" list.remove("a");//将特定的元素("a")删除 //迭代器 Iterator it=a.iterator(); while(it.hasNext()){ System.out.println(it.next()); } //集合中的a.size相当于a.length for(int i=0;i System.out.println(c));
Student s1 = new Student("小龙女", 23);
Student s2 = new Student("任盈盈", 24);
Student s3 = new Student("小龙女", 23);
Student s4 = new Student("东方不败", 25);
Student s5 = new Student("伊琳", 29);
Student s6 = new Student("周芷若", 30);
HashSet hashSet = new HashSet<>();
hashSet.add(s1);
hashSet.add(s2);
hashSet.add(s3);
hashSet.add(s4);
hashSet.add(s5);
hashSet.add(s6);//增加数据
hashSet.remove("东方不败");//删除"东方不败"
for (Student student : hashSet) {
System.out.println(student.getName()+"=="+student.getAge());
}
}
map
Map < String,Integer >map=new ConcurrentHashMap();
map.put("bab",1);
map.put("mam",2);//增加元素
map.remove(1);//删除索引1指向的元素
map.keySet().forEach(c->System.out.println(c));//只输出键
map.values().forEach(c->System.out.println(c));//只输出值
map.entrySet().forEach(c->System.out.println(c));//都输出



