【问题标题】:Objects are being overwritten each time i iterate over the loop每次我遍历循环时,对象都会被覆盖
【发布时间】:2015-02-05 05:15:30
【问题描述】:

每次我添加一个新的学生对象的迭代,它都会覆盖以前的对象并在整个列表中包含相同的对象。我尝试使用链接列表和数组列表,但仍然遇到同样的问题。如果有人愿意指导我正确的方向,我将不胜感激。谢谢你。

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.displayList(studentList),事实上你能和我们分享你的Student
  • 如果你提供完整的代码就好了,因为这似乎是个奇怪的问题。
  • @hanumant 我所说的“相同对象”是最新的。因此,当我使用 tostring 打印对象列表时 - 它只会为每次迭代打印最新的“最近对象”。
  • @hanumant 似乎无法找到 displayList 的问题...我也对其进行了调试,在我看来,每次将对象添加到列表中...所有以前的条目似乎都是我最近输入的同一个对象..

标签: java arraylist linked-list


【解决方案1】:

问题是您将class Student 的私有成员定义为static。任何将它们标记为static 的理由。请删除静态,您的代码应该可以正常工作。

【讨论】:

    【解决方案2】:

    这是一个相当特殊的问题。我最好的建议是改变

    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));
    

    【讨论】:

    • 最终你在这两种情况下都将新对象添加到列表中,所以我认为这不会有所作为。问题出在用户列出数组
    【解决方案3】:

    问题在于您的列表代码而不是添加方法,请检查您的迭代代码,如果您使用常规 for 进行迭代,则为

    for(int i=0;i

    那么您一定犯了经典的初学者错误,即为每次迭代获取第一个项目,而不是使用分配的变量,例如

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

    我建议您使用 enhanced for a.k.a foreachfor 这种情况

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 2015-07-13
      • 1970-01-01
      • 2019-06-08
      • 2023-02-23
      • 2011-02-22
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      相关资源
      最近更新 更多