JAVA学习笔记(二十五)

泛型Genericimport java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;/* * 泛型Generic * 泛型集合:即声明集合时指定集合中元素的类型 * */public class Test02{(String[] args){test3();}(){// 创建泛型集合,,保存整数List<Integer> list = new ArrayList<Integer>();list.add(12);list.add(250);list.add(13);// 遍历集合for (Integer i : list){System.out.println(i);}}(){// 创建泛型集合,保存狗狗信息List<Dog> dogs = new ArrayList<Dog>();dogs.add(new Dog(“小涛”, “雄”, 85));dogs.add(new Dog(“程程”, “雌”, 13));dogs.add(new Dog(“笑笑”, “雄”, 23));// 遍历集合for (int i = 0; i < dogs.size(); i++){Dog dog = dogs.get(i);// 返回Dog对象System.out.println(dog);}// 泛型迭代器IteratorIterator<Dog> it = dogs.iterator();while (it.hasNext()){Dog dog = it.next();// 直接返回Dog对象,无需做类型转换System.out.println(dog);}}(){// 创建泛型Map集合Map<Integer, Dog> dogs = new HashMap<Integer, Dog>();dogs.put(001, new Dog(“军子”, “雄”, 50));dogs.put(007, new Dog(“超子”, “雌”, 78));Set<Integer> keys=dogs.keySet();Iterator<Integer> it=keys.iterator();while(it.hasNext()){int id=it.next();Dog dog=dogs.get(id);System.out.println(“编号:”+id+”,信息:”+dog.toString());}}}/* * 狗狗类 */{String brand;(String name, String sex, int health){super(name, sex, health);}public Dog(String name, String sex, int health, String brand){this(name, sex, health);this.brand = brand;}(){System.out.println(“姓名:” + name + “,性别:” + sex + “,健康值:” + health+ “,品种:” + brand);}(){System.out.println(“正在接飞盘。。。。”);health = health + 5;}public String toString(){return “Dog[name=” + name + “,sex=” + sex + “,health=” + health + “]”;}}

这里的风景美不胜收,真让人流连忘返。

JAVA学习笔记(二十五)

相关文章:

你感兴趣的文章:

标签云: