如何从数组中删除元素

时间:2022-06-02 21:22:35

I have an array for example:

我有一个数组例如:

String [][] test = {{"a","1"},
                    {"b","1"},
                    {"c","1"}};

Can anyone tell me how to remove an element from the array. For example I want to remove item "b", so that the array looks like:

任何人都可以告诉我如何从数组中删除元素。例如,我想删除项“b”,以便数组看起来像:

{{"a","1"},
 {"c","1"}}

I can't find a way of doing it. What I have found here so far is not working for me :(

我找不到办法。到目前为止我在这里发现的不适合我:(

4 个解决方案

#1


10  

You cannot remove an element from an array. The size of a Java array is determined when the array is allocated, and cannot be changed. The best you can do is:

您无法从数组中删除元素。 Java数组的大小是在分配数组时确定的,不能更改。你能做的最好的事情是:

  • Assign null to the array at the relevant position; e.g.

    在相关位置为数组赋值null;例如

    test[1] = null;
    

    This leaves you with the problem of dealing with the "holes" in the array where the null values are. (In some cases this is not a problem ... but in most cases it is.)

    这使您无法处理数组中空值为“空洞”的问题。 (在某些情况下,这不是问题......但在大多数情况下都是如此。)

  • Create a new array with the element removed; e.g.

    创建一个删除了元素的新数组;例如

    String[][] tmp = new String[test.length - 1][];
    int j = 0;
    for (int i = 0; i < test.length; i++) {
        if (i != indexOfItemToRemove) {
            tmp[j++] = test[i];
        }
    }
    test = tmp;
    

    The Apache Commons ArrayUtils class has some static methods that will do this more neatly (e.g. Object[] ArrayUtils.remove(Object[], int), but the fact remains that this approach creates a new array object.

    Apache Commons ArrayUtils类有一些静态方法可以更整齐地执行此操作(例如Object [] ArrayUtils.remove(Object [],int),但事实仍然是这种方法创建了一个新的数组对象。

A better approach would be to use a suitable Collection type. For instance, the ArrayList type has a method that allows you to remove the element at a given position.

更好的方法是使用合适的Collection类型。例如,ArrayList类型有一个方法,允许您删除给定位置的元素。

#2


6  

There is no built-in way to "remove" items from a regular Java array.

没有内置方法可以从常规Java数组中“删除”项目。

What you want to use is an ArrayList.

你想要使用的是一个ArrayList。

#3


2  

You could set the entry in the array to null (test[0][1] = null;). However, "removing" the item such that the array will have one element less than before is not doable without recreating the array. If you plan to change data in the data structure regularly an ArrayList (or another Collection class depending on your needs) might be more convenient.

您可以将数组中的条目设置为null(test [0] [1] = null;)。但是,如果不重新创建数组,“删除”项目使得数组将比以前少一个元素是不可行的。如果您计划定期更改数据结构中的数据,则ArrayList(或其他Collection类,具体取决于您的需要)可能更方便。

#4


0  

My solution is:

我的解决方案是:

You cannot remove an element from an array => it's correct, but we can do something to change current array.

你无法从数组中删除元素=>它是正确的,但我们可以做一些事情来改变当前的数组。

No need assign null to the array at the relevant position; e.g.

test[1] = null;

Create a new array with the element removed; e.g.

String[][] temp = new String[test.length - 1][];

Need to get index at string/array to remove: IndexToRemove

需要获取字符串/数组的索引才能删除:IndexToRemove

for (int i = 0; i < test.length-1; i++) {
                if (i<IndexToRemove){
                    temp[i]=test[i];
                }else if (i==IndexToRemove){
                    temp[i]=test[i+1];
                }else {
                    temp[i]=test[i+1];
                }
}
test = temp;

Hope it helpful!

希望它有用!

#1


10  

You cannot remove an element from an array. The size of a Java array is determined when the array is allocated, and cannot be changed. The best you can do is:

您无法从数组中删除元素。 Java数组的大小是在分配数组时确定的,不能更改。你能做的最好的事情是:

  • Assign null to the array at the relevant position; e.g.

    在相关位置为数组赋值null;例如

    test[1] = null;
    

    This leaves you with the problem of dealing with the "holes" in the array where the null values are. (In some cases this is not a problem ... but in most cases it is.)

    这使您无法处理数组中空值为“空洞”的问题。 (在某些情况下,这不是问题......但在大多数情况下都是如此。)

  • Create a new array with the element removed; e.g.

    创建一个删除了元素的新数组;例如

    String[][] tmp = new String[test.length - 1][];
    int j = 0;
    for (int i = 0; i < test.length; i++) {
        if (i != indexOfItemToRemove) {
            tmp[j++] = test[i];
        }
    }
    test = tmp;
    

    The Apache Commons ArrayUtils class has some static methods that will do this more neatly (e.g. Object[] ArrayUtils.remove(Object[], int), but the fact remains that this approach creates a new array object.

    Apache Commons ArrayUtils类有一些静态方法可以更整齐地执行此操作(例如Object [] ArrayUtils.remove(Object [],int),但事实仍然是这种方法创建了一个新的数组对象。

A better approach would be to use a suitable Collection type. For instance, the ArrayList type has a method that allows you to remove the element at a given position.

更好的方法是使用合适的Collection类型。例如,ArrayList类型有一个方法,允许您删除给定位置的元素。

#2


6  

There is no built-in way to "remove" items from a regular Java array.

没有内置方法可以从常规Java数组中“删除”项目。

What you want to use is an ArrayList.

你想要使用的是一个ArrayList。

#3


2  

You could set the entry in the array to null (test[0][1] = null;). However, "removing" the item such that the array will have one element less than before is not doable without recreating the array. If you plan to change data in the data structure regularly an ArrayList (or another Collection class depending on your needs) might be more convenient.

您可以将数组中的条目设置为null(test [0] [1] = null;)。但是,如果不重新创建数组,“删除”项目使得数组将比以前少一个元素是不可行的。如果您计划定期更改数据结构中的数据,则ArrayList(或其他Collection类,具体取决于您的需要)可能更方便。

#4


0  

My solution is:

我的解决方案是:

You cannot remove an element from an array => it's correct, but we can do something to change current array.

你无法从数组中删除元素=>它是正确的,但我们可以做一些事情来改变当前的数组。

No need assign null to the array at the relevant position; e.g.

test[1] = null;

Create a new array with the element removed; e.g.

String[][] temp = new String[test.length - 1][];

Need to get index at string/array to remove: IndexToRemove

需要获取字符串/数组的索引才能删除:IndexToRemove

for (int i = 0; i < test.length-1; i++) {
                if (i<IndexToRemove){
                    temp[i]=test[i];
                }else if (i==IndexToRemove){
                    temp[i]=test[i+1];
                }else {
                    temp[i]=test[i+1];
                }
}
test = temp;

Hope it helpful!

希望它有用!