ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-05-21 18:31:07  阅读:202  来源: 互联网

标签:10 int Collection st add Student new 存入 public


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有