Java 删除数组元素 - 将所有元素向下移动一个索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20264372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:41:02  来源:igfitidea点击:

Delete array element - move all elements down one index

javaarrayssorting

提问by Colin747

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?

目前,如果我删除第一个元素(索引 0),它会被删除,但如果我向数组添加信息,它会在索引 1 处输入,索引 0 为空。如果从包含以下索引信息的索引中删除某些内容,我如何将所有信息向下移动一个索引?

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]

我在数组中添加了四个条目: [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]

我运行 then program 并尝试删除 index [2],这是成功输出以下内容: [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]

如您所见,元素根据需要向下移动,问题是当我再次向数组添加值时,已删除的索引被传递并且元素被输入到下一个索引中,见下文: [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;
    }
  }

采纳答案by Colin747

This was my final solution:

这是我的最终解决方案:

static int nameArrayCount, markArrayCount = 0;

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

回答by Sergey Morozov

Try use System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

尝试使用 System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

You can to see java.util.ArrayList.remove(int index)method implementation for more details.

您可以查看java.util.ArrayList.remove(int index)方法实现以获取更多详细信息。

回答by SpiderShlong

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.

我重写了您的代码,但添加了一个临时数组,该数组存储除第一个索引位置中已删除元素以外的所有值。这应该删除元素并将数组移动到最左边的位置,最右边的位置 left 为空。

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

回答by Dev Blanked

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)

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

回答by Luke

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.

您可以创建 size = StudentsArray-1 的临时数组,然后复制 studentArray 中的所有元素,但必须删除的元素除外。

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;