
代码如下:
import java.util.ArrayListimport java.util.Iterator
class Student {
private int id
private String name
private int age
private int score
public Student(int id, String name, int age, int score) {
this.id = id
this.name = name
this.age = age
this.score = score
}
public int getId() {
return id
}
public void setId(int id) {
this.id = id
}
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 int getScore() {
return score
}
public void setScore(int score) {
this.score = score
}
}
public class App {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>()
// 添加学生
students.add(new Student(1001, "小赵", 20, 400))
students.add(new Student(1002, "小钱", 21, 750))
students.add(new Student(1003, "小孙", 18, 670))
students.add(new Student(1004, "小李", 19, 550))
Iterator<Student> it = students.iterator()
while (it.hasNext()) {
Student stu = it.next()
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getScore())
}
}
}
运行结果:
List<Student>arraylist=new arrayList<Student>()假如你把学生对象放到这个arraylist里面去了,输出学生对象的属性值
for(Student student : arrarylist){
System.out.println("学生对象的属性值:"+student.get****())
}
你可以自己是试试
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)