代码之家  ›  专栏  ›  技术社区  ›  user225269

删除arraylist中的元素时出现问题

  •  1
  • user225269  · 技术社区  · 14 年前

    我正在用arraylist做一个简单的程序。但我遇到了一个错误。 删除arraylist中的元素后,使用以下代码:

    delnum=scanz.nextLine();
    intdelnum=Integer.parseInt(delnum);
    
    nem.remove(intdelnum);
    corz.remove(intdelnum);
    skul.remove(intdelnum);
    gen.remove(intdelnum);
    

    do {
        System.out.println("Add Records");
    
        System.out.print("Name: ");
        nem.add(ctr, scanz.nextLine());
    
        System.out.print("Course: ");
        corz.add(ctr, scanz.nextLine());
    
        System.out.print("Gender: ");
        gen.add(ctr, scanz.nextLine());
    
        System.out.print("School: ");
        skul.add(ctr, scanz.nextLine());
    
        System.out.println("Another?\n1.Yes\n2.No");
        adds=scanz.nextLine();
        addagain=Integer.parseInt(adds);
    
        ctr++;
    } while(addagain==1);
    

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 3
    

    请帮帮我,

    4 回复  |  直到 14 年前
        1
  •  1
  •   YoK    14 年前

    在这种情况下,在添加ArrayList时不需要维护索引。

    好的设计应该是创建一个学生对象,其中包含诸如姓名、课程、地址等详细信息,然后将学生对象添加到Arraylist中。

    public class Student {
        private String name;
        private String course;
        private String gender;
        private String school;
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCourse() {
            return course;
        }
        public void setCourse(String course) {
            this.course = course;
        }
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public String getSchool() {
            return school;
        }
        public void setSchool(String school) {
            this.school = school;
        }
    
    }
    

    然后您的代码将更改为:

        do {
            Student student = new Student();
            System.out.println("Add Records");
    
            System.out.print("Name: ");
            student.setName(scanz.nextLine());
    
            System.out.print("Course: ");
            student.setCourse(scanz.nextLine());
    
            System.out.print("Gender: ");
            student.setGender(scanz.nextLine());
    
            System.out.print("School: ");
            student.setSchool(scanz.nextLine());
    
            // Add student to students ArrayList
            students.add(student);
    
            System.out.println("Another?\n1.Yes\n2.No");
            adds = scanz.nextLine();
            addagain = Integer.parseInt(adds);
        } while (addagain == 1);
    

    希望这有帮助。

        2
  •  4
  •   the.duckman    14 年前

    那怎么办

     ctr--;
    

    删除时?

        3
  •  2
  •   Will A    14 年前

    add(object) 而不是 add(index, object) -那你就不会有上面遇到的问题了。

        4
  •  0
  •   asela38    14 年前

    在我看来,每次插入一个项目后,Ctr都会增加。但删除后不减少。因此,下一次当您将一个项目添加到这些列表中时,由ctr表示的索引在列表中不存在。这就是您获取IndexOutOfBoundException的原因。

    通过使用ctr--in删除,正如“达克曼”所说,解决了您现有的问题。但是“Will A”的方法简化了代码。正如奥斯卡的剃刀所暗示的,我喜欢用“威尔A”的答案