簡體   English   中英

每次迭代循環時,對象都會被覆蓋

[英]Objects are being overwritten each time i iterate over the loop

在每次迭代中,我添加一個新的Student對象,它將覆蓋之前的對象,並在整個列表中包含相同的對象。 我嘗試使用鏈接列表和數組列表,但仍然遇到相同的問題。 如果有人能很好地指導我朝正確的方向發展,我將不勝感激。 謝謝。

public static void main(String[] args) {

        // Scanner and LinkedList
        Scanner keyboard = new Scanner(System.in);
        LinkedList<Student> studentList = new LinkedList<Student>();
        ArrayList<Student> stdList = new ArrayList<Student>();

        int choice;
        boolean flag = true;
        do {
            Student.showMenu();
            choice = keyboard.nextInt();
            switch (choice) {
            case 1: {
                System.out.println("What is the students last name?");
                String last = keyboard.next();

                System.out.println("What is the students first name?");
                String first = keyboard.next();

                System.out.println("What is the students course code?");
                int courseCode = keyboard.nextInt();

                System.out.println("What is the students course grade?");
                String grade = keyboard.next();

                Student st = new Student(last, first, courseCode, grade);
                studentList.add(st);
                stdList.add(st);
                break;
            }
            case 2: {
                System.out
                        .println("Please enter the students last name you wish to delete.");
                String last = keyboard.next();
                int index = Student.indexOf(studentList, last);
                if (index != -1)
                    studentList.remove(index);
                else
                    System.out.println("Student does not exist.");
                break;
            }
            case 3: {
                System.out
                        .println("Please enter the students last name you wish to search.");
                String last = keyboard.next();
                int index = Student.indexOf(studentList, last);
                if (index != -1)
                    studentList.get(index).toString();
                else
                    System.out.println("Student does not exist.");
                break;
            }
            case 4: {
                System.out.println("");
                break;
            }
            case 5:
                System.out.println("");
                break;
            case 6:
                Student.displayList(studentList);
                break;
            case 7:
                flag = false;
                break;
            }
        } while (flag);

    }




public class Student {
    private static String last;
    private static String first;
    private static int courseCode;
    private static String grade;

public Student(String last, String first, int courseCode, String grade) {
    this.last = last;
    this.first = first;
    this.courseCode = courseCode;
    this.grade = grade;
}

public String getLast() {
    return last;
}

public void setLast(String last) {
    this.last = last;
}

public String getFirst() {
    return first;
}

public void setFirst(String first) {
    this.first = first;
}

public int getCourseCode() {
    return courseCode;
}

public void setCourseCode(int courseCode) {
    this.courseCode = courseCode;
}

public String getGrade() {
    return grade;
}

public void setGrade(String grade) {
    this.grade = grade;
}

public static void showMenu() {
    System.out.println("Welcome to the database menu!\n");
    System.out.println("Press 1 to insert a new record");
    System.out.println("Press 2 to delete a record");
    System.out.println("Press 3 to search the database (by last name)");
    System.out.println("Press 4 to print a range in the database");
    System.out.println("Press 5 to find the class average for a course");
    System.out.println("Press 6 to print the list");
    System.out.println("Press 7 to quit");
}

public String toString() {
    String str = " Last name: " + last + " First name: " + first
            + " Course code: " + courseCode + " Course grade: " + grade;
    return str;

}

static int indexOf(LinkedList<Student> list, String last) {
    int i = 0;
    for (Student std : list) {
        if (std.getLast().equals(last))
            return i;
        i++;
    }
    return -1;
}

static void displayList(LinkedList<Student> list) {
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i).toString() + " ");

    }
}

static void displayList(ArrayList<Student> stdList) {
    for (int i = 0; i < stdList.size(); i++) {
        System.out.println(stdList.get(i).toString() + " ");

    }

   }

}

那是一個相當奇怪的問題。 我最好的建議是改變

Student st = new Student(last, first, courseCode, grade);
studentList.add(st);
stdList.add(st);

studentList.add(new Student(last, first, courseCode, grade));
stdList.add(new Student(last, first, courseCode, grade));

問題是你上市代碼不添加方法,請檢查您的迭代的代碼,如果你使用的是常規的迭代for

for(int i = 0; i

那么您必須提交經典的初學者錯誤,該錯誤是每次迭代都獲取第一項,而不是使用分配的變量,例如

List.get(0)而不是List.get(i)

我建議您針對此類實例將增強功能也稱為foreach

問題是您已將class Student私有成員定義為static 任何理由將它們標記為static 請刪除靜態代碼,您的代碼應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM