📄 studentarraylist.java
字号:
import java.util.*;
/**
* This class contains methods to process array lists of {@link Student}
* objects.
*
* @author LiGuoLiang
* @version 1.0.0
* @see Student
* @see ArrayList
*/
public class StudentArrayList {
/**
* Returns an array list with three elements.
*
* @param first a <code>Student</code> object.
* @param second a <code>Student</code> object.
* @param third a <code>Student</code> object.
* @return an array list with the objects <code>first</code>,
* <code>second</code>, and <code>third</code>
*/
public static ArrayList<Student> makeArrayList(
Student first,
Student second,
Student third) {
/* PLACE YOUR CODE HERE */
ArrayList<Student> array = new ArrayList<Student>();
array.add(first);
array.add(second);
array.add(third);
return array; // REMOVE; USED SO THIS FILE COMPILES
}
/**
* Returns an array list with the same elements of the specified array
* arranged in the same order.
*
* @param array an array with <code>Student</code> objects .
* @return an array list with the same elements of the specified array
* arranged in the same order
*/
public static ArrayList<Student> makeArrayListFromArray(Student[] array) {
/* PLACE YOUR CODE HERE */
ArrayList<Student> anotherArray = new ArrayList<Student>();
for(Student element : array) {
anotherArray.add(element);
}
return anotherArray; // REMOVE; USED SO THIS FILE COMPILES
}
/**
* Returns <code>true</code> if the specified array list contains a
* student whose id matches the specified ID.
*
* @param arrayList an array list of <code>Student</code> objects.
* @param id a student ID.
* @return <code>true</code> if the specified array list contains a
* student whose ID matches the specified ID;
* <code>false</code> otherwise.
*/
public static boolean hasStudent(
ArrayList<Student> arrayList,
int id) {
/* PLACE YOUR CODE HERE */
for(Student element : arrayList) {
if(element.getId()==id) {
return true;
}
}
return false; // REMOVE; USED SO THIS FILE COMPILES
}
/**
* Returns the number of students in the specified array list whose
* grade is greater than or equal to the specified grade.
*
* @param arrayList an array list of <code>Student</code> objects.
* @param grade a grade.
* @return the number of students in the specified array list whose
* grade is greater than or equal to the specified grade.
*/
public static int countGradeGreaterOrEqual(
ArrayList<Student> arrayList,
int grade) {
/* PLACE YOUR CODE HERE */
int number = 0;
for(Student element : arrayList) {
if(element.getGrade()>=grade) {
number++;
}
}
return number; // REMOVE; USED SO THIS FILE COMPILES
}
/**
* Returns the smallest grade of the students in the specified array list.
* <p>
* This method assumes that the array list is not empty.
*
* @param arrayList an array list of <code>Student</code> objects.
* @return the smallest grade of the students in the specified array list.
*/
public static int getMinGrade(ArrayList<Student> arrayList) {
/* PLACE YOUR CODE HERE */
int minGrade = 0;
Iterator<Student> arrayIterator = arrayList.iterator();
if(arrayIterator.hasNext()) {
minGrade = arrayIterator.next().getGrade();
}
for(;arrayIterator.hasNext();) {
Student element = arrayIterator.next();
if(element.getGrade()<minGrade) {
minGrade = element.getGrade();
}
}
return minGrade; // REMOVE; USED SO THIS FILE COMPILES
}
/**
* Returns the average grade of the students in the specified array list.
*
* @param arrayList an array list of <code>Student</code> objects.
* @return the average grade of the students in the specified array list.
*/
public static double getGradeAverage(ArrayList<Student> arrayList) {
/* PLACE YOUR CODE HERE */
int sum =0;
double average;
for(Student element : arrayList) {
sum += element.getGrade();
}
average = ((double)sum)/arrayList.size();
return average; // REMOVE; USED SO THIS FILE COMPILES
}
/**
* Removes all students in the specified array list whose grade
* is less than the specified grade.
*
* @param arrayList an array list of <code>Student</code> objects.
* @param grade a grade.
*/
public static void removeGradeLess(
ArrayList<Student> arrayList,
int grade) {
/* PLACE YOUR CODE HERE */
Iterator<Student> arrayIterator = arrayList.iterator();
ArrayList<Student> temp = new ArrayList<Student>();
for(;arrayIterator.hasNext();) {
Student element = arrayIterator.next();
if(element.getGrade()<grade) {
temp.add(element);
}
}
for(int i=0;i<temp.size();i++) {
arrayList.removeAll(temp);
}
}
/**
* Returns the string representation of the objects in the specified
* array list.
* <p>
* A new line character ( \n ) should separate the string
* representations of the objects. For example:
* </p>
* <pre>
* Student[328,Galileo Galilei,80]\nStudent[123,Albert Einstein,100]
* </pre>
* <p>
* Note that the string does <i>not</i> end with a new line character ( \n )
* </p>
*
* @param arrayList an array list of <code>Student</code> objects.
* @return the string representation of the objects in the specified
* array list.
*/
public static String displayAll(ArrayList<Student> arrayList) {
/* PLACE YOUR CODE HERE */
String result = "";
for(int i=0;i<arrayList.size()-1;i++) {
result += arrayList.get(i).toString()+"\n";
}
result += arrayList.get(arrayList.size()-1).toString();
return result; // REMOVE; USED SO THIS FILE COMPILES
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -