声明一个ArrayList,存储一条学生信息,内容为 1 张三 22 男,将信息进行遍历出来
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class student{
public static void main(String[] args) {
ArrayList jiHe = new ArrayList();
xueSheng xueSheng1 = new xueSheng(1,"张三",22,"男");
jiHe.add(xueSheng1);
for (int i = 0; i < jiHe.size(); i++) {
System.out.println(jiHe.get(i).toString());
}
}
}class xueSheng{
int bianhao;String name;int age;String sex;
public xueSheng(int bianhao, String name, int age, String sex) {
super();
this.bianhao = bianhao;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "xueSheng [bianhao=" + bianhao + ", name=" + name + ", age="
+ age + ", sex=" + sex + "]";
}
}
声明一个学生类,包含编号,名称,年龄,和性别属性,
实例化5名学生对象,将其保存在Set集合中,设置Set集合的泛型为SetXueSheng,遍历该Set集合
package list;
import java.util.*;
import java.util.Map.Entry;
public class set {
public static void main(String[] args) {
HashSet jiHe=new HashSet();
SetXueSheng duiXiang1=new SetXueSheng(1,"诗书三连",18,"男");
SetXueSheng duiXiang2=new SetXueSheng(2,"画唱点赞",19,"男");
SetXueSheng duiXiang3=new SetXueSheng(3,"诗书关注",20,"男");
SetXueSheng duiXiang4=new SetXueSheng(4,"画唱收藏",21,"男");
SetXueSheng duiXiang5=new SetXueSheng(5,"诗书画唱",22,"男");
jiHe.add(duiXiang1);
//第一个都要先放到集合中,之后才用Collections.addAll的方法。
//自己总结的语法:Collections.addAll(集合名,对象名1,对象名2,......)
Collections.addAll(jiHe,duiXiang2,duiXiang3,duiXiang4,duiXiang5);
for(SetXueSheng i:jiHe){
System.out.println(i);
}
}
}
class SetXueSheng{
public int bianh