开发者

How to properly delete elements in java arrays

I have a simple program that lets you add, edit, list, search and delete records by storing it on arrays.

My problem now, is how to properly delete items on it. Here is my current code.

System.out.print("Input number to delete: ");
                    delnum=scanz.nextLine();
                    intdelnum=Integer.parseInt(delnum);

 name[intdelnum]=null;
                            course[intdelnum]=null;
                            gender[intdelnum]=null;
                            school[intdelnum]=null;

As you can see, if I list this array which contains 10 elements each. The array index which Ive set to null, will remain null forever. And I cannot add anything on it. Do you know of a better way on how to do this. So that when I delete the array element at index 2. The element at index 3 will go up. to index 2? So that later I could stil开发者_StackOverflow中文版l add an element on the index which I've set to null.


You could do it manually by shifting the array contents around, or by using an array copying call like System.arraycopy, but you almost certainly want to use a collection instead. ArrayList is a good choice for a mutable array. ArrayList.remove will let you remove by index:

ArrayList<...> name, course, gender, school;

intdelnum = Integer.parseInt(delnum);
name.remove(intdelnum);
course.remove(intdelnum);
gender.remove(intdelnum);
school.remove(intdelnum);

Some other random notes about your code snippet:

I'm assuming scanz is a Scanner. You can call nextInt() on it instead of using nextLine() and converting the String to an int.

You also probably want to make a class that holds all that information, instead of using four parallel arrays to track it:

class Student {
    enum Gender {MALE, FEMALE}

    private String name;
    private Course course; // This can be a String if that's simpler for your project
    private Gender gender;
    private School school; // See note for 'course'
}

ArrayList<Student> students;


I would use an ArrayList for this.

It will allow you to easily remove items from your list, shifting every item behind it down

ArrayList<String> myArrayList;

/*... initialization and filling in of your data ...*/

myArrayList.remove(index);

For full reference here is the 1.5.0 API page for ArrayList.


You can't resize the array once it is created.
You can create a List from the array like Arrays.asList(), use remove() to remove elements you don't want and then use toArray() to get back an array from the List.


You can write a method (code for delete operation ) to shift array values around.

like if element at x is deleted method would look like :

private void deleteElement(int del_idx){
    if(del_idx <= org_arr.length ){
        org_arr[del_idx]=null;
        for(int i = del_idx;i < (org_arr.length-1); i++){
            if(org_arr[i+1] != null){
                org_arr[i] = org_arr[i+1];
            }
        }
    }
}

But it will be more suitable to use ArrayList rather than array in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜