Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java运行时从数组中删除元素_Java_Arrays - Fatal编程技术网

在Java运行时从数组中删除元素

在Java运行时从数组中删除元素,java,arrays,Java,Arrays,有没有办法在运行时从数组中删除元素 例如: int[] num = {8, 1, 4, 0, 5}; Output: Enter the Index: 0 1, 4, 0, 5 Enter the Index: 3 1, 4, 0 Enter the Index: 1 4, 0; 我知道一旦数组初始化,就无法调整其长度,在这种示例问题中,使用ArrayList更为实用。但是,是否有一种方法可以通过仅使用数组来解决此类问题 通过创建新数组并复制其中原始数组的值,我成功地删除了一个元素并显示了

有没有办法在运行时从数组中删除元素

例如:

int[] num =  {8, 1, 4, 0, 5};

Output:
Enter the Index: 0
1, 4, 0, 5
Enter the Index: 3
1, 4, 0
Enter the Index: 1
4, 0;
我知道一旦数组初始化,就无法调整其长度,在这种示例问题中,使用
ArrayList
更为实用。但是,是否有一种方法可以通过仅使用数组来解决此类问题

通过创建新数组并复制其中原始数组的值,我成功地删除了一个元素并显示了数组-1。但问题是,在输出的下一次迭代中,我仍然可以删除一个元素,但大小不变

情况就是这样:

int[] num =  {8, 1, 4, 0, 5};

Output:
Enter the Index: 0
1, 4, 0, 5  // in the first loop it goes as I want it.
Enter the Index: 2
1, 4, 5, 5  // this time array's length is still 4 and just duplicates the last value
Enter the Index: 1
1, 5, 5, 5  // length is still the same and so on.
这是我从数组中删除元素的代码:

public static int[] removeElement(int index, int[] n) {

    int end = n.length;

    for(int j = index; j < end - 1; j++) {
        n[j] = n[j + 1];            
    }
    end--;

    int[] newArr = new int[end];
    for(int k = 0; k < newArr.length; k++) {
        newArr[k] = n[k];
    }

    displayArray(newArr);        

    return newArr;
}

public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     int[] num = {8, 1, 4, 0, 5};

     for(int i = 0; i < num.length; i++) {
          System.out.print("Enter the Index: ");
          int index = input.nextInt();
          removeElement(index, num);
     }
}

public static void displayArray(int[] n) {
     int i = 0;
     for(; i < n.length - 1; i++) {
          System.out.print(n[i] + ", ");
     }
     System.out.print(n[i]);
}
公共静态int[]removeElement(int索引,int[]n){
int end=n.长度;
for(int j=index;j

如何在阵列上执行此操作,有什么诀窍吗?或者我真的必须使用
ArrayList

您正在丢弃
removeElement
返回的新数组

将循环更改为:

for(int i = 0; i < num.length; i++) {
     System.out.print("Enter the Index: ");
     int index = input.nextInt();
     num = removeElement(index, num);
}
for(int i=0;i
从代码中,实际上并不是从数组中删除元素。实际上,您正在创建一个新数组,其大小比以前的数组小1,并用旧数组的剩余值填充新数组

此外,从旧数组中删除元素的逻辑是错误的。首先,您的旧数组仍然具有相同的大小,您所要做的就是将位于索引位置的数组元素替换为位于索引+1位置的元素

您可以尝试以下代码:

public static int[] removeElement(int index, int[] arr) {
    int length = arr.length - 1;
    int[] res = new int[length];
    for(int i = 0; i < index; i++) {
        res[i] = arr[i];
    }
    for(int i = index; i < length; i++) {
        res[i] = arr[i + 1];
    }
    return res;
}
公共静态int[]removeElement(int索引,int[]arr){
int length=arr.length-1;
int[]res=新的int[长度];
对于(int i=0;i


上面代码片段的想法是将数组复制到一个新的数组(长度减少1),跳过我们要删除的元素

我看到你的数组只包含正数。也许您可以将最后一个数字替换为
-1
,因此在显示数组时,您可以在数组上运行,直到得到值
-1
@Eran doneeditting@robert,这是一个建议。。最好使用Java集合。。。