如何从java中的字符串数组中删除特定值? [重复]

时间:2022-08-26 14:54:51

Possible Duplicate:
Removing an element from an Array (Java)

可能重复:从数组中删除元素(Java)

How to remove specific String array value for example

例如,如何删除特定的String数组值

String[] str_array = {"item1","item2","item3"};

String [] str_array = {“item1”,“item2”,“item3”};

i want to remove "item2" from str_array pls help me i want output like

我想从str_array中删除“item2”请帮助我,我希望输出像

String[] str_array = {"item1","item3"};

String [] str_array = {“item1”,“item3”};

4 个解决方案

#1


16  

I would do it as follows:

我会这样做:

String[] str_array = {"item1","item2","item3"};
List<String> list = new ArrayList<String>(Arrays.asList(str_array));
list.remove("item2");
str_array = list.toArray(new String[0]);

#2


6  

If you must use arrays, System.arraycopy is the most efficient, scalable solution. However, if you must remove one element from an array several times, you should use an implementation of List rather than an array.

如果必须使用数组,System.arraycopy是最有效,可扩展的解决方案。但是,如果必须多次从数组中删除一个元素,则应使用List的实现而不是数组。

The following utilizes System.arraycopy in order to achieve the desired effect.

以下使用System.arraycopy以实现所需的效果。

public static Object[] remove(Object[] array, Object element) {
    if (array.length > 0) {
        int index = -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(element)) {
                index = i;
                break;
            }
        }
        if (index >= 0) {
            Object[] copy = (Object[]) Array.newInstance(array.getClass()
                    .getComponentType(), array.length - 1);
            if (copy.length > 0) {
                System.arraycopy(array, 0, copy, 0, index);
                System.arraycopy(array, index + 1, copy, index, copy.length - index);
            }
            return copy;
        }
    }
    return array;
}

Also, you can increase the method's efficiency if you know that your array consists of only Comparable objects. You can use Arrays.sort to sort them before passing them through the remove method, modified to use Arrays.binarySearch to find index rather than a for loop, raising that portion of the method's efficiency from O(n) to O(nlogn).

此外,如果您知道阵列仅包含Comparable对象,则可以提高方法的效率。您可以使用Arrays.sort对它们进行排序,然后再将它们传递给remove方法,修改为使用Arrays.binarySearch查找索引而不是for循环,将方法效率的该部分从O(n)提高到O(nlogn)。

#3


4  

You could use the ArrayUtils API to remove it.

您可以使用ArrayUtils API删除它。

array = ArrayUtils.removeElement(array, element);

#4


1  

Other Option is to copy array to other array accept than remove item.

其他选项是将数组复制到其他数组接受而不是移除项目。

 public static String[] removeItemFromArray(String[] input, String item) {
    if (input == null) {
        return null;
    } else if (input.length <= 0) {
        return input;
    } else {
        String[] output = new String[input.length - 1];
        int count = 0;
        for (String i : input) {
            if (!i.equals(item)) {
                output[count++] = i;
            }
        }
        return output;
    }
}

#1


16  

I would do it as follows:

我会这样做:

String[] str_array = {"item1","item2","item3"};
List<String> list = new ArrayList<String>(Arrays.asList(str_array));
list.remove("item2");
str_array = list.toArray(new String[0]);

#2


6  

If you must use arrays, System.arraycopy is the most efficient, scalable solution. However, if you must remove one element from an array several times, you should use an implementation of List rather than an array.

如果必须使用数组,System.arraycopy是最有效,可扩展的解决方案。但是,如果必须多次从数组中删除一个元素,则应使用List的实现而不是数组。

The following utilizes System.arraycopy in order to achieve the desired effect.

以下使用System.arraycopy以实现所需的效果。

public static Object[] remove(Object[] array, Object element) {
    if (array.length > 0) {
        int index = -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(element)) {
                index = i;
                break;
            }
        }
        if (index >= 0) {
            Object[] copy = (Object[]) Array.newInstance(array.getClass()
                    .getComponentType(), array.length - 1);
            if (copy.length > 0) {
                System.arraycopy(array, 0, copy, 0, index);
                System.arraycopy(array, index + 1, copy, index, copy.length - index);
            }
            return copy;
        }
    }
    return array;
}

Also, you can increase the method's efficiency if you know that your array consists of only Comparable objects. You can use Arrays.sort to sort them before passing them through the remove method, modified to use Arrays.binarySearch to find index rather than a for loop, raising that portion of the method's efficiency from O(n) to O(nlogn).

此外,如果您知道阵列仅包含Comparable对象,则可以提高方法的效率。您可以使用Arrays.sort对它们进行排序,然后再将它们传递给remove方法,修改为使用Arrays.binarySearch查找索引而不是for循环,将方法效率的该部分从O(n)提高到O(nlogn)。

#3


4  

You could use the ArrayUtils API to remove it.

您可以使用ArrayUtils API删除它。

array = ArrayUtils.removeElement(array, element);

#4


1  

Other Option is to copy array to other array accept than remove item.

其他选项是将数组复制到其他数组接受而不是移除项目。

 public static String[] removeItemFromArray(String[] input, String item) {
    if (input == null) {
        return null;
    } else if (input.length <= 0) {
        return input;
    } else {
        String[] output = new String[input.length - 1];
        int count = 0;
        for (String i : input) {
            if (!i.equals(item)) {
                output[count++] = i;
            }
        }
        return output;
    }
}