99

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

jjnguy
  • 136,852
  • 53
  • 295
  • 323
NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • 1
    possible duplicate of [How do I remove objects from an Array in java?](http://stackoverflow.com/questions/112503/how-do-i-remove-objects-from-an-array-in-java) – McDowell May 09 '11 at 10:28
  • 5
    Not a dupe. The previous question is about removing items by value; this is about removing an item by index. – james.garriss Nov 26 '13 at 15:22

6 Answers6

183

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(oldArr, 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
marczoid
  • 1,365
  • 2
  • 12
  • 20
jjnguy
  • 136,852
  • 53
  • 295
  • 323
16

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.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
mikera
  • 105,238
  • 25
  • 256
  • 415
5

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
  • 278,309
  • 50
  • 514
  • 539
3

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.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
msw
  • 42,753
  • 9
  • 87
  • 112
-1

To sum up, the quick linkedlist method:

List<String> llist = new LinkedList<String>(Arrays.asList(oldArray));
llist.remove(0);
mjad-org
  • 39
  • 4
-13

An alternative ugly method:

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