You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建学生ArrayList数据库遇addStudent()异常,求排查与功能建议

Hey Luke, let's break down what's going wrong with your addStudent() function first, then tackle those four feature requests you have.


The Core Issue with Your Current Code

Your problem stems from two critical mistakes:

  1. Static student array: You're using a static Object[] student variable, which means every call to newStudent() modifies the same array object. When you add this array to your database list multiple times, you're just adding multiple references to that single array—so by the end, every entry in database points to the last student you created.
  2. Incorrect database population: Your newStudent() method uses database.addAll(Arrays.asList(student)), which adds each element of the student array as a separate entry in the list (instead of adding the entire array as one student record). Then addStudent() adds the array reference on top of that, making your database a messy mix of individual values and array references.

Fixed Base Code

Let's fix those issues first by removing the static array and cleaning up how we add records to the database:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;

public class Record {
    // Database stores entire student arrays (one array per student)
    static List<Object[]> database = new ArrayList<>();

    // Student info variables (unused right now since you're using Object arrays)
    private int studentID;
    private String lastName;
    private String firstName;
    private long phoneNumber;
    private String major;
    private double gpa;
    private int birthYear;
    private String birthMonth;
    private int birthDay;
    private String homeAddress;

    // Setters can stay if you want to use them later with a proper Student class
    public void setStudentID(int idGiven) { studentID = idGiven; }
    public void setLastName(String lastNameGiven) { lastName = lastNameGiven; }
    public void setFirstName(String firstNameGiven) { firstName = firstNameGiven; }
    public void setPhoneNumber(long numGiven) { phoneNumber = numGiven; }
    public void setMajor(String majorGiven) { major = majorGiven; }
    public void setGPA(double gpaGiven) { gpa = gpaGiven; }
    public void setBirthYear(int birthYearGiven) { birthYear = birthYearGiven; }
    public void setBirthMonth(String birthMonthGiven) { birthMonth = birthMonthGiven; }
    public void setBirthDay(int birthDayGiven) { birthDay = birthDayGiven; }
    public void setHomeAddress(String homeAddressGiven) { homeAddress = homeAddressGiven; }

    // Create a NEW student array each time (no static variable!)
    static public Object[] newStudent(int newStudentID, String newLastName, String newFirstName, long newPhoneNumber,
                                      String newMajor, double newGPA, int newBirthYear, String newBirthMonth,
                                      int newBirthDay, String newHomeAddress) {
        Object[] student = new Object[10];
        student[0] = newStudentID;
        student[1] = newLastName;
        student[2] = newFirstName;
        student[3] = newPhoneNumber;
        student[4] = newMajor;
        student[5] = newGPA;
        student[6] = newBirthYear;
        student[7] = newBirthMonth;
        student[8] = newBirthDay;
        student[9] = newHomeAddress;
        return student;
    }

    // Add the student array to the database (now each entry is a unique student)
    static public void addStudent(Object[] s) {
        database.add(s);
    }

Implementing Your Requested Features

Now let's add the four functions you need:

1. Delete Record by Student ID

// Delete student by ID
    static public void deleteStudentByID(int targetID) {
        database.removeIf(student -> (int) student[0] == targetID);
    }

2. List All Students in Storage Order

// List all students in the order they were added
    static public void listAllStudents() {
        System.out.println("All Students (Storage Order):");
        for (Object[] student : database) {
            System.out.println(Arrays.toString(student));
        }
        System.out.println();
    }

3. Sort by ID, GPA, or Birthday (Ascending/Descending)

We'll create a helper method to handle month-to-number conversion for birthday sorting, plus dedicated methods for each sort type:

// Helper to convert month string to integer for sorting
    private static int getMonthNumber(String month) {
        return switch (month.toLowerCase()) {
            case "january" -> 1;
            case "february" -> 2;
            case "march" -> 3;
            case "april" -> 4;
            case "may" -> 5;
            case "june" -> 6;
            case "july" -> 7;
            case "august" -> 8;
            case "september" -> 9;
            case "october" -> 10;
            case "november" -> 11;
            case "december" -> 12;
            default -> 0;
        };
    }

    // Sort by ID (ascending or descending)
    static public void sortByID(boolean ascending) {
        Collections.sort(database, (s1, s2) -> {
            int id1 = (int) s1[0];
            int id2 = (int) s2[0];
            return ascending ? Integer.compare(id1, id2) : Integer.compare(id2, id1);
        });
    }

    // Sort by GPA (ascending or descending)
    static public void sortByGPA(boolean ascending) {
        Collections.sort(database, (s1, s2) -> {
            double gpa1 = (double) s1[5];
            double gpa2 = (double) s2[5];
            return ascending ? Double.compare(gpa1, gpa2) : Double.compare(gpa2, gpa1);
        });
    }

    // Sort by Birthday (ascending or descending)
    static public void sortByBirthday(boolean ascending) {
        Collections.sort(database, (s1, s2) -> {
            int year1 = (int) s1[6];
            int year2 = (int) s2[6];
            if (year1 != year2) {
                return ascending ? Integer.compare(year1, year2) : Integer.compare(year2, year1);
            }
            int month1 = getMonthNumber((String) s1[7]);
            int month2 = getMonthNumber((String) s2[7]);
            if (month1 != month2) {
                return ascending ? Integer.compare(month1, month2) : Integer.compare(month2, month1);
            }
            int day1 = (int) s1[8];
            int day2 = (int) s2[8];
            return ascending ? Integer.compare(day1, day2) : Integer.compare(day2, day1);
        });
    }

4. Filter Students by Major or ID

// Filter students by major OR ID
    static public List<Object[]> filterStudents(String targetMajor, Integer targetID) {
        List<Object[]> filtered = new ArrayList<>();
        for (Object[] student : database) {
            int studentID = (int) student[0];
            String major = (String) student[4];
            // Check if either major matches OR ID matches (if targetID is not null)
            if (major.equalsIgnoreCase(targetMajor) || (targetID != null && studentID == targetID)) {
                filtered.add(student);
            }
        }
        return filtered;
    }

    // Helper to print filtered students
    static public void printFilteredStudents(List<Object[]> filtered) {
        System.out.println("Filtered Students:");
        for (Object[] student : filtered) {
            System.out.println(Arrays.toString(student));
        }
        System.out.println();
    }

Updated Main Method to Test Everything

public static void main(String[] args) {
        // Create students
        Object[] student1 = Record.newStudent(00000000, "Thomas", "Luke", 9038189888L, "Computer Science", 4.00, 1997, "February", 28, "169 Karen Dr.");
        Object[] student2 = Record.newStudent(00000001, "Doe", "Jane", 5802679999L, "Mathematics", 3.62, 1990, "March", 1, "100 Circle Dr.");
        Object[] student3 = Record.newStudent(00000002, "Baker", "Joe", 9805776452L, "Safety", 2.75, 1998, "December", 7, "200 Circle Dr.");

        // Add to database
        Record.addStudent(student1);
        Record.addStudent(student2);
        Record.addStudent(student3);

        // Test 1: List all students
        listAllStudents();

        // Test 2: Delete student with ID 00000001
        deleteStudentByID(00000001);
        System.out.println("After deleting ID 00000001:");
        listAllStudents();

        // Re-add student2 for further tests
        addStudent(student2);

        // Test 3: Sort by GPA descending
        sortByGPA(false);
        System.out.println("Sorted by GPA (Descending):");
        listAllStudents();

        // Test 4: Filter by major "Computer Science"
        List<Object[]> csStudents = filterStudents("Computer Science", null);
        printFilteredStudents(csStudents);

        // Test 5: Filter by ID 00000002
        List<Object[]> id2Students = filterStudents(null, 00000002);
        printFilteredStudents(id2Students);
    }
}

A Quick Suggestion for Better Code

Using Object[] for student records works, but it's error-prone (easy to mix up indices, no type safety). A better approach is to create a dedicated Student class with typed fields and getters/setters. If you're open to that, I can share a refactored version!

内容的提问来源于stack exchange,提问作者LukeThomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:11:18