Java基础--集合(代码)

xiaoxiao2021-02-27  304

package com.xayd_01; //在集合中存储3个学生对象,并遍历,拿出每一个学生对象的属性。 public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(){} public Student(String name,int age){ this.name=name; this.age=age; } } package com.xayd_01; import java.util.ArrayList; import java.util.Collection; //在集合中存储3个学生对象,并遍历,拿出每一个学生对象的属性。 /** * 1.创建学生对象 * 2.创建collection集合并添加元素 * 3.将collection集合转化为数组 * 4.使用for循环遍历数组 * */ public class StudentDemo { public static void main(String[] args) { //1.创建学生对象 Student s1 = new Student("甲",20); Student s2 = new Student("乙",22); Student s3 = new Student("丙",23); // 2.创建collection集合并添加元素 Collection c = new ArrayList(); c.add(s1); c.add(s2); c.add(s3); // 3.将collection集合转化为数组 Object[] arr = c.toArray(); //4.遍历数组 for (int i = 0; i < arr.length; i++) { //将arr[i]强制转化为Student类型 Student s = (Student) arr[i]; System.out.println(s.getAge()+" "+s.getName()); } } } package com.xayd_02; public class Dog { private String name; private int age; public Dog() { super(); // TODO Auto-generated constructor stub } public Dog(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } package com.xayd_02; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; //创建狗对象(带参数),存储到集合,用迭代器进行遍历并打印对象的属性数据 /** * 1.创建一个狗的对象 * 2.创建一个集合 * 3.用iterator创建一个迭代器 * 4.使用next方法进行遍历 */ public class DogDemo { public static void main(String[] args) { // 1.创建一个狗的对象 Dog d1 = new Dog("芊芊",2); Dog d2 = new Dog("乐乐",3); Dog d3 = new Dog("哈哈",1); // 2.创建一个集合 Collection <Dog> c = new ArrayList<Dog>(); c.add(d1); c.add(d2); c.add(d3); // 3.用iterator创建一个迭代器 Iterator <Dog> it = c.iterator(); // 4.使用next方法进行遍历 while(it.hasNext()){ Dog d = it.next(); System.out.println(d.getAge()+" "+d.getName()); } } } package com.xayd_04; public class Car { private String brand; private String color; private int price; public Car(String brand, String color, int price) { super(); this.brand = brand; this.color = color; this.price = price; } public Car() { super(); // TODO Auto-generated constructor stub } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } } package com.xayd_04; import java.util.ArrayList; import java.util.Iterator; import java.util.List; //用List集合存储3个汽车对象,然后遍历。 /** * 1.创建汽车类对象 * 2.创建list集合并添加对象 * 3.获取相应迭代器,并调用相关方法进行遍历 * 4.使用for,foreach进行遍历 * */ public class CarTest { public static void main(String[] args) { // 1.创建汽车类对象 Car c1 = new Car("丰田","红色",120000); Car c2 = new Car("本田","蓝色",140000); Car c3 = new Car("尼桑","银色",120000); // 2.创建list集合并添加对象 List <Car> list = new <Car> ArrayList(); list.add(c1); list.add(c2); list.add(c3); // 3.获取相应迭代器,并调用相关方法进行遍历 Iterator <Car> it = list.iterator(); while(it.hasNext()){ Car c = it.next(); System.out.println(c.getBrand()+" "+c.getPrice()+" "+c.getColor()); } System.out.println("---------------------------------------"); // 3.使用普通for循环 for (int i = 0; i < list.size(); i++) { Car c = list.get(i); System.out.println(c.getBrand()+" "+c.getColor()+" "+c.getPrice()); } System.out.println("----------------------------------------------"); // 3.使用增强for for(Car c:list){ System.out.println(c.getBrand()+" "+c.getColor()+" "+c.getPrice()); } } } package com.xayd00; public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } package com.xayd00; import java.util.HashSet; public class HashSetDemo { public static void main(String[] args) { //创建一个HashSet集合 HashSet<Student> hs = new HashSet<Student>(); //创建学生对象 Student s1 = new Student ("周杰伦",35); Student s2 = new Student ("陈奕迅",40); Student s3 = new Student ("薛之谦",30); Student s4 = new Student ("杨宗纬",32); Student s5 = new Student ("林俊杰",36); //把学生对象添加到HashSet集合中 hs.add(s1); hs.add(s2); hs.add(s3); hs.add(s4); hs.add(s5); //遍历集合 for (Student S: hs) { System.out.println(S.getName()+" "+S.getAge()); } } } package com.xayd01; import java.util.TreeSet; //创建集合存储Integer类型的元素(20,18,23,22,17,24,19,18,24)并遍历 public class TreeSetDemo { public static void main(String[] args) { //创建一个TreeSet集合 TreeSet <Integer> ts = new TreeSet <Integer>(); //用add方法给集合中添加元素 ts.add(20); ts.add(18); ts.add(23); ts.add(22); ts.add(17); ts.add(24); ts.add(19); ts.add(18); ts.add(24); for (Integer in : ts) { System.out.print(in+" "); } } } //17 18 19 20 22 23 24 package com.xayd02; import java.util.HashMap; import java.util.Set; public class HashMapDemo { public static void main(String[] args) { // 3.存入(Student,String) //创建一个Map集合 HashMap<Student, String> hm = new HashMap<Student, String>(); //创建学生对象 Student s1 = new Student("杰克逊", 60); Student s2 = new Student("孙楠", 50); Student s3 = new Student("权志龙", 30); Student s4 = new Student("权志龙", 30); //将对象存入集合 hm.put(s1, "美国"); hm.put(s2, "中国"); hm.put(s3, "韩国"); hm.put(s4, "中国"); //遍历集合 Set<Student> keys = hm.keySet(); for (Student s : keys) { System.out.println(s+" "+hm.get(s)); } } }
转载请注明原文地址: https://www.6miu.com/read-4527.html

最新回复(0)