从包含特定数字</student>的arrayList <student>中删除一行

时间:2013-11-18 21:57:28

标签: java class methods arraylist

代码在这里:http://pastie.org/8490904

public class Project5_test {

static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // initialize an arary that holds N objects of type Student
        int end = 1;
        ArrayList<Student> studentList = new ArrayList<Student>();
        ArrayList<Student> removeArray = new ArrayList<Student>();
        // read in the data
        for (int y = 0; y < end;) {
            String nextMove = nextMove();
            for (int i = 0; i < end;) {
                if ("add".equalsIgnoreCase(nextMove)) {
                    System.out.println("Student Input: ");
                    String firstName = getFirstName();
                    String lastName = getLastName();
                    int uid = getUid();
                    StudentType type = inputStudentType();
                    Student base = new Student(firstName, lastName, uid, type);
                    if (type == StudentType.UNDECLARED) {
                        ClassStanding standing = inputClassStanding();
                        studentList.add(base);
                    } else if (type == StudentType.UNDERGRADUATE) {
                        ClassStanding standing = inputClassStanding();
                        Major major = inputMajor();
                        double overallGpa = getOverallGPA();
                        String overallGpaDouble = Double.toString(overallGpa);
                        double majorGpa = getOverallGPA();
                        String majorGpaDouble = Double.toString(majorGpa);
                        studentList.add(new Student.UnderGraduate(
                                new Student(firstName, lastName, uid, type),
                                major, overallGpa, majorGpa, standing));
                    } else if (type == StudentType.GRADUATE) {
                        boolean thesis = getThesisStatus();
                        ClassStanding studyType = getStudy();
                        String profName = getProfName();
                        studentList.add(new Student.Graduate(
                                new Student(firstName, lastName, uid, type),
                                thesis, studyType, profName));
                    }
                }
                i++;
            }

            if ("remove".equalsIgnoreCase(nextMove)) {
                int removeUid = getRemoveUid();
                removeArray = remove(studentList, removeUid);
                for (int i = 0; i < removeArray.size(); i++) {
                    System.out.println(removeArray.get(i));
                }
            }

            if ("save".equalsIgnoreCase(nextMove)) {
                for (int i = 0; i < removeArray.size(); i++) {
                    System.out.println(removeArray.get(i));
                }
                y++;
            }
        }
    }

    enum ClassStanding {

        FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, UNKNOWN,
        MASTERS_STUDIES, PHD_STUDIES, NO_STANDING
    };

    enum StudentType {

        UNDERGRADUATE, GRADUATE, UNDECLARED
    };

    enum Major {

        CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN
    };

    public static class Student {

        public String firstName;     // first name
        public String lastName;
        public double uid;      // last name
        public StudentType type;

        public Student(Student orig) {
            this.firstName = orig.firstName;
            this.lastName = orig.lastName;
            this.uid = orig.uid;
            this.type = orig.type;
        }

        // construct a new student with given fields
        public Student(String firstName, String lastName, Integer newUid, StudentType type) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.uid = newUid;
            this.type = type;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getLastName() {
            return lastName;
        }
        //set type

        public void setType(StudentType type) {
            this.type = type;
        }
        //return type

        public StudentType getType() {
            return type;
        }

        // return a string representation of the invoking object
        public String toString() {
            return firstName + " " + lastName + " " + uid + " " + type;
        }

        public static class Graduate extends Student {

            public boolean thesis;
            public ClassStanding study;
            public String profName;

            public Graduate(Student orig, boolean isThesis, ClassStanding study, String profName) {
                super(orig);
                thesis = isThesis;
                this.study = study;
                this.profName = profName;
            }

            public boolean getThesis() {
                return thesis;
            }

            public void setThesis(Boolean thesis) {
                this.thesis = thesis;
            }

            public ClassStanding getStudy() {
                return study;
            }

            public void setStudy(ClassStanding study) {
                this.study = study;
            }

            public String getProfName() {
                return profName;
            }

            public void setProfName(String profName) {
                this.profName = profName;
            }

            public String toString() {
                return super.toString() + thesis + " " + study + " " + profName;
            }
        }

        public static class UnderGraduate extends Student {

            public Major major;
            public Double overallGpa;
            public Double majorGpa;
            public ClassStanding study;

            public UnderGraduate(Student orig, Major major, Double overallGpa, Double majorGpa, ClassStanding study) {
                super(orig);
                this.study = study;
                this.major = major;
                this.overallGpa = overallGpa;
                this.majorGpa = majorGpa;
            }

            public void setMajor(Major major) {
                this.major = major;
            }
            //return type

            public Major getmMajor() {
                return major;
            }

            public void setOverallGPA(Double overallGpa) {
                this.overallGpa = overallGpa;
            }

            public Double getOverallGPA() {
                return overallGpa;
            }

            public void setMajorGPA(Double majorGpa) {
                this.majorGpa = majorGpa;
            }

            public Double getMajorGPA() {
                return majorGpa;
            }

            public ClassStanding getStudy() {
                return study;
            }

            public void setStudy(ClassStanding study) {
                this.study = study;
            }

            public String toString() {
                return study + " " + major + " " + overallGpa + " " + majorGpa;
            }
        }
    }

    public static String getFirstName() {
        Scanner input = new Scanner(System.in);
        String firstName;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students first name: ");
                firstName = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a string please enter another first name!");
                input.next();
                continue;
            }
        }
        return firstName;
    }

    public static String getLastName() {
        Scanner input = new Scanner(System.in);
        String lastName;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter a students last name: ");
                lastName = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a string please enter another last name!");
                input.next();
                continue;
            }
        }
        return lastName;
    }

    public static Integer getUid() {
        Scanner input = new Scanner(System.in);
        int uid;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter a uid number: ");
                uid = input.nextInt();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer!");
                input.next();
                continue;
            }
        }
        return uid;
    }

    public static StudentType inputStudentType() {
        Scanner input = new Scanner(System.in);
        String type;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Is the student a graduate, undergraduate, or, undeclared: ");
                type = input.next();

                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Is the student a graduate, undergraduate, or, undeclared: ");
                type = input.next();
                continue;
            }
        }
        if ("undergraduate".equalsIgnoreCase(type)) {
            StudentType status = StudentType.UNDERGRADUATE;
            return status;
        }
        if ("graduate".equalsIgnoreCase(type)) {
            StudentType status = StudentType.GRADUATE;
            return status;
        }
        if ("undeclared".equalsIgnoreCase(type)) {
            StudentType status = StudentType.UNDECLARED;
            return status;
        }
        return null;

    }

    public static String nextMove() {
        Scanner input = new Scanner(System.in);
        String nextMove;
        while (true) {
            // prompt the user to enter if there are more characers
            try {
                System.out.print("What now? Add, Sort, Remove, Save: ");
                nextMove = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("More input? true or false(type true or false): ");
                input.next();
                continue;
            }
        }
        return nextMove;
    }

    public static ClassStanding inputClassStanding() {
        Scanner input = new Scanner(System.in);
        String type;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the students class standing:  ");
                type = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Input the students class standing: ");
                type = input.next();
                continue;
            }
        }
        if ("Freshman".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.FRESHMAN;

            return standing;
        }
        if ("sophomore".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.SOPHOMORE;

            return standing;
        }
        if ("junior".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.JUNIOR;

            return standing;
        }
        if ("senior".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.SENIOR;
            return standing;
        }
        if ("unknown".equalsIgnoreCase(type)) {
            ClassStanding standing = ClassStanding.UNKNOWN;
            return standing;
        }
        return null;

    }

    public static Major inputMajor() {
        Scanner input = new Scanner(System.in);
        String major;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN):  ");
                major = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("False input. Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN): ");
                major = input.next();
                continue;
            }
        }
        if ("cs".equalsIgnoreCase(major)) {
            Major finalMajor = Major.CS;
            return finalMajor;
        }
        if ("ceg".equalsIgnoreCase(major)) {
            Major finalMajor = Major.CEG;
            return finalMajor;
        }
        if ("ee".equalsIgnoreCase(major)) {
            Major finalMajor = Major.EE;
            return finalMajor;
        }
        if ("ise".equalsIgnoreCase(major)) {
            Major finalMajor = Major.ISE;
            return finalMajor;
        }
        if ("bme".equalsIgnoreCase(major)) {
            Major finalMajor = Major.BME;
            return finalMajor;
        }
        if ("ME".equalsIgnoreCase(major)) {
            Major finalMajor = Major.ME;
            return finalMajor;
        }
        if ("met".equalsIgnoreCase(major)) {
            Major finalMajor = Major.MET;
            return finalMajor;
        }
        if ("unknown".equalsIgnoreCase(major)) {
            Major finalMajor = Major.UNKNOWN;
            return finalMajor;
        }
        return null;

    }

    public static Double getOverallGPA() {
        Scanner input = new Scanner(System.in);
        double overallGpa;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students overall GPA: ");
                overallGpa = input.nextDouble();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer! Please enter the students overall GPA:");
                input.next();
                continue;
            }
        }
        return overallGpa;
    }

    public static Double getMajorlGPA() {
        Scanner input = new Scanner(System.in);
        double majorGpa;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter the students major GPA: ");
                majorGpa = input.nextDouble();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer! Please enter the students major GPA:");
                input.next();
                continue;
            }
        }
        return majorGpa;
    }

    public static boolean getThesisStatus() {
        Boolean thesis;
        Scanner input = new Scanner(System.in);
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter TRUE for having a thesis option else FALSE: ");
                thesis = input.nextBoolean();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a boolean response. True or false, is there a thesis: ");
                input.next();
                continue;
            }
        }
        return thesis;
    }

    public static ClassStanding getStudy() {
        String study = null;
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 1;) {
            while (true) {
                //prompt the user to enter the popularity number for each character
                try {
                    System.out.print("Enter Masters for Master Studies or Phd for Phd studies: ");
                    study = input.next();
                    break;
                    //error handling
                } catch (InputMismatchException e) {
                    System.out.println("Not a valid response. Enter Masters for Master Studies or Phd for Phd studies: ");
                    input.next();
                    continue;
                }
            }
            if ("masters".equalsIgnoreCase(study)) {
                ClassStanding standing = ClassStanding.MASTERS_STUDIES;
                return standing;
            }
            if ("phd".equalsIgnoreCase(study)) {
                ClassStanding standing = ClassStanding.PHD_STUDIES;
                return standing;
            }
        }
        return null;
    }

    public static String getProfName() {
        String name;
        Scanner input = new Scanner(System.in);
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Enter the name of the major professor: ");
                name = input.next();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a valid response. Enter the name of the major professor: ");
                input.next();
                continue;
            }
        }
        return name;
    }

    public static int getRemoveUid() {

        Scanner input = new Scanner(System.in);
        int removeUid;
        while (true) {
            //prompt the user to enter the popularity number for each character
            try {
                System.out.print("Please enter a uid number to be removed: ");
                removeUid = input.nextInt();
                break;
                //error handling
            } catch (InputMismatchException e) {
                System.out.println("Not a number or an integer!");
                input.next();
                continue;
            }
        }

        return removeUid;
    }

    public static ArrayList<Student> remove(ArrayList<Student> array, Integer uid) {

        for (int i = 0; i < array.size(); i++) {
            array.get(i);
            if (i == uid) {
                array.remove(i);
                break;
            }
        }
        return array;

    }
}

给出了删除函数的学生uid编号,将删除具有该编号的该学生的整个arrayList。  例如:derp lerp 222未定的新生 如果删除下一个动作并且用户输入222 结果:(不再是给定uid号码的学生)

代码末尾的方法remove();是固定的,可能的 if(“remove”.equalsIgnoreCase(nextMove))在main方法

当我执行我的代码时,使用remove()方法

后数组保持不变

1 个答案:

答案 0 :(得分:2)

使用remove方法

public static ArrayList<Student> remove(ArrayList<Student> array, Integer uid) {

    for (int i = 0; i < array.size(); i++) {
        array.get(i);
        if (i == uid) {
            array.remove(i);
            break;
        }
    }
    return array;

}

您要将iuid进行比较,同时应将学生ID与uid进行比较。尝试类似这样的事情(getID将是返回学生ID的方法。如果你没有,那么你应该创建它。)

for (int i = 0; i < array.size(); i++) {
    if (uid.equals(array.get(i).getID())) {//I am using equals since uid is Object
                                           //and I want to compare its value, not
                                           //reference
        array.remove(i);
        break;
    }
}