声明一个 Student 类,属性包括姓名、学号、总成绩;生成10个 Student 类对象,并放在一个一维数组中,编写方法按总成绩进行排序,将排序后的对象分别保持在 Vector 、 ArrayList 、 HashTable 类型的对象中,并遍历显示其中元素的信息。 程序设计参考框架如下: import java . util . Vector ; import java . util . ArrayList ; import java . util . Hashtable ; import java . util . Enumeration ; import java . util . Iterator ; public class Ex71{ public static void main ( String [] args ){ /* *定义 Student 类型的数组并初始化 *对数组中的元素按照总成绩从高第低排序 *将排序好的数组对象依次存入 Vector *将排序好的数组对象依次存入 ArrayList,*将排序好的数组对象依次存入 HashTable *对集合中的元素进行遍历打印输出 class Student ( private String xh ; private String xm ; private float zf ; public Student ( String xh , String xm , float zf ){ super (); this . xh = xh ; this . xm = xm ; this . zf = zf ; public String getxh (){ return xh ; public void setXh ( String xh )( this . xh = xh ; public String getxm () return xm ; public void setXm ( String xm ) this . xm = xm ; public float getzf (){ return zf ; public void setzf ( float zf ){ this . zf = zf ; public String toString ()( return "学号:"+ xh +"姓名:"+ xm +"总成绩:"+
时间: 2023-06-10 10:03:53 浏览: 158
zf ) ;
Student[] students = new Student[10];
// 初始化10个学生对象
students[0] = new Student("001", "张三", 90.5f);
students[1] = new Student("002", "李四", 85.0f);
students[2] = new Student("003", "王五", 92.5f);
students[3] = new Student("004", "赵六", 88.0f);
students[4] = new Student("005", "钱七", 95.5f);
students[5] = new Student("006", "孙八", 89.0f);
students[6] = new Student("007", "周九", 91.5f);
students[7] = new Student("008", "吴十", 87.5f);
students[8] = new Student("009", "郑一", 93.0f);
students[9] = new Student("010", "冯二", 86.5f);
// 对数组进行排序
for (int i = 0; i < students.length; i++) {
for (int j = i + 1; j < students.length; j++) {
if (students[i].getzf() < students[j].getzf()) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
// 创建 Vector 对象并将排序后的学生对象存入
Vector<Student> vector = new Vector<Student>();
for (int i = 0; i < students.length; i++) {
vector.add(students[i]);
}
// 创建 ArrayList 对象并将排序后的学生对象存入
ArrayList<Student> arrayList = new ArrayList<Student>();
for (int i = 0; i < students.length; i++) {
arrayList.add(students[i]);
}
// 创建 Hashtable 对象并将排序后的学生对象存入
Hashtable<String, Student> hashtable = new Hashtable<String, Student>();
for (int i = 0; i < students.length; i++) {
hashtable.put(students[i].getxh(), students[i]);
}
// 遍历 vector 中的学生对象并输出信息
System.out.println("Vector 中的学生对象:");
for (int i = 0; i < vector.size(); i++) {
Student student = vector.get(i);
System.out.println(student.toString());
}
// 遍历 arrayList 中的学生对象并输出信息
System.out.println("ArrayList 中的学生对象:");
Iterator<Student> arrayIterator = arrayList.iterator();
while (arrayIterator.hasNext()) {
Student student = arrayIterator.next();
System.out.println(student.toString());
}
// 遍历 hashtable 中的学生对象并输出信息
System.out.println("HashTable 中的学生对象:");
Enumeration<Student> hashEnumeration = hashtable.elements();
while (hashEnumeration.hasMoreElements()) {
Student student = hashEnumeration.nextElement();
System.out.println(student.toString());
}
阅读全文