views:

192

answers:

5

I have string array (String[]) and I need to remove the first item. How can I do that efficiently?

+5  A: 

Simplest way is probably as follows - you basically need to construct a new array that is one element smaller, then copy the elements you want to keep to the right positions.

int n=oldArray.length-1;
String[] newArray=new String[n];
System.arraycopy(oldArray,1,newArray,0,n);

Note that if you find yourself doing this kind of operation frequently, it could be a sign that you should actually be using a different kind of data structure, e.g. a linked list. Constructing a new array every time is an O(n) operation, which could get expensive if your array is large. A linked list would give you O(1) removal of the first element.

An alternative idea is not to remove the first item at all, but just increment an integer that points to the first index that is in use. Users of the array will need to take this offset into account, but this can be an efficient approach. The Java String class actually uses this method internally when creating substrings.

mikera
This is not technically the easiest way. `Arrays.copyOfRange()` is.
jjnguy
Since he is using Java6, he can use the more compact Arrays.copyOfRange
Thilo
@Justin - sure but only if you are targeting Java 1.6 or above
mikera
@mikera, true. It isn't always applicable.
jjnguy
@mikera - the title of the question makes it clear that the OP **is** interested in answers for Java 1.6 and above.
Stephen C
+7  A: 

The size of arrays in Java cannot be changed. So, technically you cannot remove any elements from the array.

One way to simulate removing an element from the array is to create a new, smaller array, and then copy all of the elements from the original array into the new, smaller array.

String[] yourArray = Arrays.copyOfRange(yourArray, 1, oldArr.length);

However, I would not suggest the above method. You should really be using a List<String>. Lists allow you to add and remove items from any index. That would look similar to the following:

List<String> list = new ArrayList<String>(); // or LinkedList<String>();
list.add("Stuff");
// add lots of stuff
list.remove(0); // removes the first item
jjnguy
It is important to note that removing the first element of an `ArrayList` is O(n).
Matthew Flaschen
@Matt, for an array and the list. But, the code is way easier for the list.
jjnguy
For an array and an `ArrayList`, but not for `LinkedList`.
Matthew Flaschen
@matt, oh yeah. I always forget about that option.
jjnguy
+2  A: 

You can't do it at all, let alone quickly. Arrays in Java are fixed size. Two things you could do are:

  1. Shift every element up one, then set the last element to null.
  2. Create a new array, then copy it.

You can use System.arraycopy for either of these. Both of these are O(n), since they copy all but 1 element.

If you will be removing the first element often, consider using LinkedList instead. You can use LinkedList.remove, which is from the Queue interface, for convenience. With LinkedList, removing the first element is O(1). In fact, removing any element is O(1) once you have a ListIterator to that position. However, accessing an arbitrary element by index is O(n).

Matthew Flaschen
+2  A: 

Keep an index of the first "live" element of the array. Removing (pretending to remove) the first element then becomes an O(1) time complexity operation.

msw
A: 

An alternative ugly method:

   String[] a ={"BLAH00001","DIK-11","DIK-2","MAN5"};
   String[] k=Arrays.toString(a).split(", ",2)[1].split("]")[0].split(", ");
Emil