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 still add an element on the index which I’ve set to null.
I would use an
ArrayListfor this.It will allow you to easily remove items from your list, shifting every item behind it down
For full reference here is the 1.5.0 API page for
ArrayList.