Array Lists and Generics

时间:2015-07-31 20:26:24

标签: java generics arraylist

I nee help with the following question:

Define a class called StudentDatabaseArrayList which uses a generic type T that must implement Sortable use this to create a data base of students. The class should include the following:

• An ArrayList to save objects of some class that implements Sortable.

• A method insert(T newObject) that inserts a newObject in the appropriate position in the ArrayList, so that the ArrayList is a sorted list, where the sorting order is decided by the method lessThan in the interface. For instance, if T is the class you defined in Assignment 3, this will create a list of students, such that all students who are Canadian seniors appear first. Then all the Canadian regular students appear, followed by all foreign students. Within each group, the students must be sorted according to their names.

• A method toString() which returns the description of all students in the system, using the sorting order described above.

• A method retrieveStudentsInRange(T lowerLimit, T upperLimit) which returns the description of all students lying in between the lower limit and the upper limit.

I have already created the Student classes (Student, regular Student, foreign Student, senior student) Here is what student looks like:

public abstract class Student implements sortable {
private int studentNumber;
private String studentName;
private Course courses[];
private Student bestFriend;
private int numCourses;
private int takenNum;

private static int num = 1;

public Student(String studentName, int maxNum) {
    this.studentName = studentName;
    courses = new Course[maxNum];
    numCourses = maxNum;
    studentNumber = num;
    num++;
    takenNum = 0;
    bestFriend = null;
}

public Student(String studentName) {
    this(studentName, 4);
}

public String getName() {
    return studentName;
}

public abstract double computeFees();

public abstract String findCountry();

public int getTakenNum() {
    return takenNum;
}

public boolean insertCourse(Course aCourse) {
    for (int i = 0; i < takenNum; i++) {
        if (aCourse.courseCodeMatches(courses[i]))
            return false;
    }
    if (takenNum < numCourses || numCourses == 0) {
        courses[takenNum] = aCourse;
        takenNum++;
        return true;
    }
    return false;

}

public void setBestFriend(Student aStudent) {
    bestFriend = aStudent;
}

public boolean bothFriends() {
    if (this.studentName.equals(bestFriend.bestFriend.studentName))
        return true;
    return false;
}


public String toString() {
    String info = ("student # " + this.studentNumber + ", Name = " + this.studentName);
    if (this.bestFriend != null)
        info = info + ", best friend = " + this.bestFriend.studentName;
    int i = 0;
    while (i < this.takenNum) {
        info = info + "\n Course " + (i + 1) + ":   " + this.courses[i];
        i++;
    }
    info = info + " \n pays fees: $" + computeFees() + " and is from " +     findCountry() + "\n ";

    return info;
}

and Sortable looks like this:

interface sortable{
boolean lessThan(sortable anObject);
}

I'm having trouble with using generics to create the insert method for the students any hints/help would be appreciated. This is what I currently have for the Array List:

import java.util.ArrayList;
class StudentDatabaseArrayList<T> implements sortable  {
private ArrayList<T> myList;

public StudentDatabaseArrayList() {
    myList = new ArrayList<T>();
}
public void insert(T newObject) {
    if (myList.isEmpty()) {
        myList.add(0, newObject);
    }
    for (int i = 0; i < myList.size()-1; i++) {
        Student compare = (Student)myList.get(i);
        Student wantToAdd = (Student) newObject;
        while (compare.lessThan(wantToAdd)) {
            myList.add(i, newObject);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

When declaring the StudentDatabaseArrayList, this line:

class StudentDatabaseArrayList<T> implements sortable {

should be:

class StudentDatabaseArrayList<T extends sortable> {

because it is the generic type T that you need to be sortable, not the database itself.

In your insert method, this will mean you no longer have to cast the newObject as a Student - you will be able to access the lessThan method on any object of the generic type T because "T extends sortable".

答案 1 :(得分:0)

Your interface should probably look similar to this

interface sortable<T>{
    boolean lessThan(T anObject);
}