5

I have code which allows a user to delete an element from an array that they select however I then want to move all existing elements "down one" so there are no gaps left in the array.

At the moment if I delete the first element (index 0) it is deleted but if I add information to the array it is entered at index 1 and index 0 is left null. If something is deleted from an index which has information in the following indices how can I move all the information down one index?

My delete method:

  static void deleteStudent() {
    System.out.println("Wish student would you like to delete?");
    for(int i=0;i<9;i++) {
      System.out.println(i + ": " + studentNamesArray[i]);
    }
    int studentChoice = input.nextInt();
    for(int i = studentChoice+1; i<studentNamesArray.length; i++) {
      studentNamesArray[i-1] = studentNamesArray[i];
    }
  }

EDIT:

I added four entries to the array: [one, two, three, four, null, null, null, null, null, null]

I ran then program and tried to delete index [2], this was successful outputting the following: [one, two, four, null, null, null, null, null, null, null]

as you can see the the elements where moved down as wanted, the problem is then when I add a value to the array again the deleted index is passed over and the element is entered into the next index, see below: [one, two, four, null, newadd, null, null, null, null, null]

How can I have the program entering the new value into the correct array?

Add method:

  static void addStudent() {
    if (nameArrayCount < 10) {
      System.out.println("Enter the student's name in the following format - surname, forename: ");
      studentName = input.next();
      studentNamesArray[nameArrayCount] = studentName;
      nameArrayCount = nameArrayCount + 1;
    }
    else if (nameArrayCount == 10) {
      System.out.println("******Array is full, please delete a student before adding another.*****");
    }
    if (markArrayCount < 10){
    System.out.println("Enter the first mark: ");
          markOne = input.nextInt();
          System.out.println("Enter the second mark: ");
          markTwo = input.nextInt();
          System.out.println("Enter the third mark: ");
          markThree = input.nextInt();
          studentMarksArray[markArrayCount][0] = markOne;
          studentMarksArray[markArrayCount][1] = markTwo;
          studentMarksArray[markArrayCount][2] = markThree;
          markArrayCount = markArrayCount + 1;
    }
  }
4
  • You could use ArrayList. Commented Nov 28, 2013 at 11:05
  • Unfortunatly for the purposes of this I cannot use ArrayList or the array import Commented Nov 28, 2013 at 11:06
  • 2
    stackoverflow.com/questions/20254914/… Commented Nov 28, 2013 at 11:08
  • Posted edited with new information. Commented Nov 28, 2013 at 11:21

4 Answers 4

2

I rewrote your code but added a temp array that stores all of the values except for the deleted element in the first index position onward. This should delete the element and shift the array to the left-most position with the right-most position left as null.

static void deleteStudent() {
    System.out.println("Which student would you like to delete?"); //fixed spelling error
    for(int i=0;i<9;i++) {
      System.out.println(i + ": " + studentNamesArray[i]);
    }
    int studentChoice = input.nextInt();
    StudentName[] temp = new StudentName[studentNamesArray.length];//I don't know format
    for(int i = 0; i<studentNamesArray.length; i++) {
        if(i != studentChoice)
        {
            temp[j] = studentNamesArray[i]
            j++;
        }
    }
    temp[studentNamesArray.length-1] = null;
    studentNamesArray = temp;
}
Sign up to request clarification or add additional context in comments.

7 Comments

I just edited my original post as you added this, will this solve my updated problem?
No, I didn't see an issue in your delete method initially just gave an alternate solution that I knew would work. Give me a bit to look through add method and I'll edit if I find the problem.
Thanks, I think it could be something to do with nameArrayCount as I'm adding 1 each time an element is added so if I add 4 then the count is at 3 but if I delete [1] the count is still at 3 so when I add a new student the count goes to 4.
I've added nameArrayCount = nameArrayCount -1; to the end of the delete method and it seems to work.
I was about to post that :). Also, unless you count 0 as a student you should do "studentNamesArray[nameArrayCount-1] = studentName;" or you might reach out of bounds at max name count
|
1

This was my final solution:

static int nameArrayCount, markArrayCount = 0;

  static void deleteStudent() {
    System.out.println("Which student would you like to delete?");
    for(int i=0;i<10;i++) {
      System.out.println(i + ": " + studentNamesArray[i]);
    }
    int studentChoice = input.nextInt();
    for(int i = studentChoice+1; i<studentNamesArray.length; i++) {
      studentNamesArray[i-1] = studentNamesArray[i];
    }
    nameArrayCount = nameArrayCount -1;
  }

Comments

0

You could declare a new array having one less element and copy the relevant values using

http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object, int, java.lang.Object, int, int)

Comments

0

You can create temporary array with size = studentsArray-1 and then copy all the elements from studentsArray except the element that has to be removed.

int studentNamesArraySize = studentNamesArray.length;
String [] temp = new String[studentNamesArray.length];
int internalCounter = 0;
for(int i=0;i<studentNamesArraySize; ++i){
    if( i != studentChoice ){
        temp[internalCounter] = studentNamesArray[i];
        internalCounter++;
    }
}
temp[temp.length] = null;
studentNamesArray = temp;

1 Comment

The problem with a temporary array is that it consumes the extra memory in the machine. OK for a homework problem but bad for a large array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.