其他分享
首页 > 其他分享> > 创建10个学生对象存入Collection集合中

创建10个学生对象存入Collection集合中

作者:互联网

package com.itheima7.ArrayList01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class ArraylestDemo03 {
public static void main(String[] args) {
Collection<Student> st=new ArrayList<>();
st.add(new Student("张三",23,91));
st.add(new Student("李四",25,91));
st.add(new Student("张五",26,93));
st.add(new Student("张三",27,93));
st.add(new Student("张三",28,83));
st.add(new Student("张三",23,87));
st.add(new Student("张三",21,91));
st.add(new Student("张三",23,97));
st.add(new Student("张三",23,82));
st.add(new Student("张三",23,88));
System.out.println(extracted(st));
geimin(st);
fail(st);
}
public static Student extracted(Collection<Student> st) {//构造一个方法,参数类型 Student
Iterator<Student> it = st.iterator(); //创建迭代器
Student st1=null;//给定一个空的Student用于储存需要返回的st1
int max=0;
while (it.hasNext()){ //判断下一个是否为空
Student stu = it.next(); //stu储存当前值 并把指针指向下一个值
if (max< stu.getScore()){//循环判断当前学生分数是否大于max
max=stu.getScore();//大于就赋值给max
st1 =stu; //并且也把值赋给需要返回的st1
}
}
return st1;//Student 內型的返回值st1
}
public static void geimin(Collection<Student> st){
Iterator<Student> it = st.iterator();
int max=0;
while (it.hasNext()){
Student next = it.next();
max+=next.getScore();
}
System.out.println("总分是:"+max+"平均分"+max/10.0);
}
public static void fail(Collection<Student> st){
Iterator<Student> it = st.iterator();
int count=0;
while (it.hasNext()){
Student next = it.next();
if (next.getScore()<60){
count++;
}
}
System.out.println(count);
}

}

class Student{
private String name;
private int age;
private int score;

public Student() {
}

public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}

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;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}

标签:10,int,Collection,st,add,Student,new,存入,public
来源: https://www.cnblogs.com/wukaijianyue/p/16295693.html