tags:

views:

55

answers:

4

I'm doing a simple program using arraylist. But I've encountered an error. After deleting an element in the arraylist, using this code:

delnum=scanz.nextLine();
intdelnum=Integer.parseInt(delnum);

nem.remove(intdelnum);
corz.remove(intdelnum);
skul.remove(intdelnum);
gen.remove(intdelnum);

I'm having problems with adding another record after a delete. From what I see, the index, where I'm storing the next element is greater than the size, since I deleted an item.

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

I get this error:

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

Please help,

+2  A: 

You should just use add(object) rather than add(index, object) - then you won't have the problem you've encoutered above.

Will A
+3  A: 

What about

 ctr--;

on delete?

the.duckman
A: 

As I see you Ctr gets increased after every time you insert an item. but not get decreased after deletion. So, next time you when you go to add an item to these lists the index represents by ctr does not exist in the list. This is the reason for you to get IndexOutOfBoundException.

By using ctr-- in deletion, as "the Duckman" said, solves the your existing problem. But "Will A"'s approach simplifies the code. As Osccam's Razor suggest I like to go with "Will A"'s answer

asela38
+1  A: 

In your case You don't need to maintain index while adding in ArrayList. I think in your case you require it as you save students information in multipple ArrayList like name in nem arraylist, course in corz arraylist ...etc. And then you use it for corelation. I think this is not very good design.

Good design would have been to create Student object with details like name, course, address etc, And then add Student object to 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;
    }

}

Then your code will change to:

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

Hope this is helpful.

YoK