java集合类

来源:互联网 发布:淘宝买苹果主板靠谱吗 编辑:程序博客网 时间:2024/06/06 20:33

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
 * 现在有一个学生类,包含学号(1-5),姓名,三门课的成绩;
 * 随机输入五个学生的信息;
 * 1、添加进一个集合中;
 * 2、计算每一个学生的三门课的平均成绩;
 * 3、遍历获取每个学生的信息;
 * 4、新加入一个学生信息(6,新晋,67,78,90);
 * 5、删除学号为奇数的学生信息;
 * 6、集合里的学生信息按照平均成绩进行降序排序;
 */
public class Test 
{
static List<Student> list = new ArrayList<Student>();
public static void main(String[] args) 
{
method1(list);//1、添加进一个集合中;
method3(list);//3、遍历获取每个学生的信息;
list.add(new Student(6, "新晋", 67,78,90));  //4、新加入一个学生信息(6,新晋,67,78,90);
method5(list);//5、删除学号为奇数的学生信息;
method6(list);//6、集合里的学生信息按照平均成绩进行降序排序;

}
// 1、添加进一个集合中;
public static void method1(List<Student> list)
{
Scanner scan = new Scanner(System.in);
int id;
String str = null;
float score1;
float score2;
float score3;
for(int i=0; i<5; i++)
{
System.out.print("input id: ");
id = scan.nextInt();
System.out.print("input name: ");
str = scan.next();
System.out.print("input score1: ");
score1 = scan.nextFloat();
System.out.print("input score2: ");
score2 = scan.nextFloat();
System.out.print("input score3: ");
score3 = scan.nextFloat();
list.add(new Student(id, str, score1, score2, score3));
System.out.println();
}
}
// 3、遍历获取每个学生的信息;
public static void method3(List<Student> list)
{
System.out.println("学号              姓名         三科成绩                平均成绩");
for(int i=0; i<list.size(); i++)
{
System.out.println(list.get(i).getId() + "\t" + list.get(i).getName() + "\t" + list.get(i).getScore1()
+ ", "+ list.get(i).getScore2() + ", " + list.get(i).getScore3() + "\t\t" + list.get(i).getAveragescore());
}
}
// 5、删除学号为奇数的学生信息;
public static void method5(List<Student> list)
{
for(int i=0; i<list.size(); i++)
{
if(list.get(i).getId()%2 == 1)
{
list.remove(i);
i--;   //若删除一个,数组长度就减小一个,i跟着减小一个;
}
}
System.out.println("删除后:");
method3(list);
}
// 6、集合里的学生信息按照平均成绩进行降序排序;
public static void method6(List<Student> list)
{
Student temp;
for(int i=0; i<list.size()-1; i++)
{
for(int j=i+1; j<list.size(); j++)
{
if(list.get(i).sortAverageScore(list.get(j)))
{
temp = list.get(j);
list.remove(j);
list.add(j, list.get(i));
list.remove(i);
list.add(i, temp);
}
}

}
System.out.println("排序后:");
method3(list);
}

}






public class Student 
{
int id;  //学号;
String name; //姓名;
float score1; //成绩1;
float score2; //成绩2;
float score3; //成绩3;
float averageScores; //平均成绩;

public Student(int id, String name, float scorce1, float scorce2, float scorce3)
{
this.id = id;
this.name = name;
this.score1 = scorce1;
this.score2 = scorce2;
this.score3 = scorce3;
this.averageScores = (score1 + score2 + score3)/3;
}

public int getId()
{
return id;
}
public String getName()
{
return name;
}
public float getScore1()
{
return score1;
}
public float getScore2()
{
return score2;
}
public float getScore3()
{
return score3;
}

public float getAveragescore()
{
return averageScores;
}
public boolean sortAverageScore(Student s)
{
if(this.averageScores >= s.averageScores)
{
return false;
}
else
{
return true;
}
}
}









































































0 0
原创粉丝点击