java数组删除数组元素_如何在Java中删除数组元素

java数组删除数组元素

When we create an array in Java, we specify its data type and size. This is used by JVM to allocates the necessary memory for array elements. There are no specific methods to remove elements from the array.

在Java中创建数组时,我们指定其数据类型和大小。 JVM使用它为数组元素分配必要的内存。 没有从数组中删除元素的特定方法。

1.使用for循环从数组中删除一个元素 (1. Removing an element from Array using for loop)

This method requires the creation of a new array. We can use for loop to populate the new array without the element we want to remove.

此方法需要创建一个新的数组。 我们可以使用for循环来填充新数组,而无需删除想要删除的元素。


package com.journaldev.java;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5};
        int[] arr_new = new int[arr.length-1];
        int j=3;
        for(int i=0, k=0;i<arr.length;i++){
            if(i!=j){
                arr_new[k]=arr[i];
                k++;
            }
        }
        System.out.println("Before deletion :" + Arrays.toString(arr));
        System.out.println("After deletion :" + Arrays.toString(arr_new));

    }
}
Arr Delete

The code removes the element at index 3. This method simply copies all the elements except the one at index 3 to a new array.

代码将删除索引3处的元素。此方法将索引3处的元素以外的所有元素简单地复制到新数组中。

2.按其值删除数组元素 (2. Deleting an array element by its value )

Unlike the previous case, this code will delete the element based on its value. This will not work with duplicates since the size of the array after deletion has to be known.

与前一种情况不同,此代码将根据其值删除该元素。 这不适用于重复项,因为必须知道删除后阵列的大小。


package com.journaldev.java;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5};
        int[] arr_new = new int[arr.length-1];
        int j=3;
        for(int i=0, k=0;i<arr.length;i++){
            if(arr[i]!=j){
                arr_new[k]=arr[i];
                k++;
            }
        }
        System.out.println("Before deletion :" + Arrays.toString(arr));
        System.out.println("After deletion :" + Arrays.toString(arr_new));

    }
}
Arr Delete based on value

The only difference between this and the previous case is arr[i]!=j in the if condition in place of i!=j.

这种情况与前一种情况之间的唯一区别是在if条件下代替i!=j arr[i]!=j i!=j

3.当数组包含重复项时,通过其值删除元素 (3. Deleting element by its value when the array contains duplicates )

Performing deletion based on the value in case of duplicates requires using ArrayList. Since ArrayList doesn’t require the size to be now in advance, it allows us to expand dynamically.

在重复的情况下,根据值执行删除操作需要使用ArrayList。 由于ArrayList不需要现在预先指定大小,因此它允许我们动态扩展。


package com.journaldev.java;

import java.util.ArrayList;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        int[] arr = new int[]{1,3,3,4,5};
        ArrayList<Integer> arr_new = new ArrayList<>();
        int j=3;
        for(int i=0;i<arr.length;i++){
            if(arr[i]!=j){
                arr_new.add(arr[i]);

            }
        }
        System.out.println("Before deletion :" + Arrays.toString(arr));
        System.out.println("After deletion :" +arr_new);

    }
}
array-deletion-duplicates

4.移位相同数组中的元素 (4. Shifting elements in the same array)

This method involves shifting the elements in the same array. Shifting of elements replaces the element to be deleted by the element at the next index.

此方法涉及移动同一阵列中的元素。 元素的移动将替换下一个索引处要删除的元素。


package com.journaldev.java;
import java.util.Arrays;
public class Main {

    public static void main(String[] args) {
        int[] arr = new int[]{1,3,3,4,5};
        int j=3;
        System.out.println("Before deletion :" + Arrays.toString(arr));
        int count =0;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] == j){
                count++;

                // shifting elements
                for(int k = i; k < arr.length - 1; k++){
                    arr[k] = arr[k+1];
                }
                i--;
               // break;
            }
        }

        System.out.print("After Deletion :" );
        for(int i = 0; i < arr.length-count; i++){
            System.out.print(" " + arr[i]);
        }
        System.out.println();
        System.out.println("Whole array :" + Arrays.toString(arr));

    }
}

Count variable indicates the number of elements deleted. This variable is essential to keep a track of index till which the array should be printed. This method takes care of duplicates as well.

Count变量指示删除的元素数。 该变量对于跟踪要打印数组的索引至关重要。 此方法也处理重复项。

Shifting Elements In Array

5.从ArrayList中删除元素 (5. Deleting elements from ArrayList)

ArrayList is backed by arrays. The deletion of an element in the ArrayList is straight forward. It requires one simple call to an inbuilt function.

ArrayList由数组支持。 直接删除ArrayList中的元素。 它仅需简单调用一个内置函数。


package com.journaldev.java;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {

    public static void main(String[] args) {
        int[] arr = new int[]{1,3,3,4,5};
        ArrayList<Integer> arr_new = new ArrayList<Integer>();
        for (int i : arr)
        {
            arr_new.add(i);
        }
        arr_new.remove(3);
        System.out.println("Before deletion :" + Arrays.toString(arr));
        System.out.println("After Deletion:" + arr_new);
    }
}

A call to the remove(i) function removes the element at index i. Deletion in ArrayLists is relatively easier as compared to Arrays.

调用remove(i)函数将删除索引i处的元素。 与Arrays相比,ArrayLists中的删除相对容易。

ArrayList Deletion

结论 (Conclusion)

We saw some examples of deleting elements in an array using different methods. The difference between the deletion of an element in an Array and an ArrayList is clearly evident. If deletion is to be performed again and again then ArrayList should be used to benefit from its inbuilt functions. Same is the case with the addition of elements to an array. ArrayList due to its dynamic nature provides easier and less cumbersome ways to alter an array.

我们看到了一些使用不同方法删除数组中元素的示例。 删除Array中的元素和ArrayList之间的区别很明显。 如果要一次又一次地执行删除操作,则应使用ArrayList来受益于其内置功能。 将元素添加到数组的情况也是如此。 ArrayList由于其动态特性而提供了更轻松,更省力的方式来更改数组。

翻译自: https://www.journaldev.com/42212/java-remove-array-elements

java数组删除数组元素

  • 32
    点赞
  • 127
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值