13

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

How to remove specific String array value for example

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

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

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

Community
  • 1
  • 1
Vicky
  • 285
  • 2
  • 4
  • 13

3 Answers3

47

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]);
Vikdor
  • 23,934
  • 10
  • 61
  • 84
7

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.

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

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).

FThompson
  • 28,352
  • 13
  • 60
  • 93
  • 1
    It's not very nice to get an `Object[]` back when you want a `String[]`. [`Array.newInstance`](http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Array.html#newInstance%28java.lang.Class,%20int%29) lets you construct an array of arbitrary type. – gustafc Oct 10 '12 at 06:13
  • @gustafc Good point. The downside to that is slightly slower execution time though. – FThompson Oct 10 '12 at 06:22
  • It's also worth noting that *O(n log n)* isn't necessarily faster than *O(n)* as Big O only tells you how the cost of handling *n+1* elements is compared to handling *n* elements with the same algorithm. The constant cost associated with sorting may well outweigh the benefits of binary search (especially for small arrays). – gustafc Oct 10 '12 at 06:22
  • `Array.newInstance` *might* be slower, but it is not necessarily so. HotSpot can optimize it to be just as fast as a regular array instantiation in most cases. Either way, you end up with a more robust and correct program. – gustafc Oct 10 '12 at 06:28
  • Ah, that's good. It was slightly slower in a small test I just conducted (average of 0.40ms for new Object[x], and average 0.45ms for Array.newInstance), but the margin is so small that I'd say it's irrelevant and may not even exist. – FThompson Oct 10 '12 at 06:30
  • 2
    Indeed. Also, if that part of the code becomes a bottleneck, arrays probably aren't the most appropriate data structure for your program. – gustafc Oct 10 '12 at 06:36
4

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;
    }
}
R H
  • 387
  • 3
  • 13