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

- 136,852
- 53
- 295
- 323

- 61,906
- 73
- 206
- 352
-
1possible 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
-
5Not 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 Answers
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
-
38It is important to note that removing the first element of an `ArrayList` is O(n). – Matthew Flaschen Sep 08 '10 at 01:27
-
1@Matt, for an array and the list. But, the code is way easier for the list. – jjnguy Sep 08 '10 at 01:28
-
18
-
-
5O(n) ? well.. in a C array? to remove the fist element you can just increment the pointer O(1) – Hernán Eche May 30 '14 at 20:16
-
if a JDK method only return "String[]" not return "List
", then copyOfRange is the better option? – Cong De Peng Mar 19 '15 at 08:01 -
@CongDePeng Well, you can use Arrays.asList to turn your array into a List. – jjnguy Mar 20 '15 at 15:21
-
3For those whom using Java for Android like me, [`Arrays.copyOfRange()`](http://developer.android.com/reference/java/util/Arrays.html#copyOfRange%28byte[],%20int,%20int%29) is for API9+ – Sdghasemi Jul 05 '15 at 09:51
-
@MatthewFlaschen nice. Just to add to it, removal of only last element would be O(1). It is because `ArrayList` internally is represented by an array. – Boss Man Jun 06 '18 at 21:03
-
@HernánEche not really no, you need to either reallocate or shift left, both operations are O(n) ;) – Mathieu_Du Nov 08 '18 at 02:07
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.
-
4
-
5
-
1
-
1
-
6the title of the question makes it clear that the OP **is** interested in answers for Java 1.6 and above. – Stephen C Sep 08 '10 at 01:59
You can't do it at all, let alone quickly. Arrays in Java are fixed size. Two things you could do are:
- Shift every element up one, then set the last element to null.
- 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).

- 278,309
- 50
- 514
- 539
To sum up, the quick linkedlist method:
List<String> llist = new LinkedList<String>(Arrays.asList(oldArray));
llist.remove(0);

- 39
- 4
An alternative ugly method:
String[] a ={"BLAH00001","DIK-11","DIK-2","MAN5"};
String[] k=Arrays.toString(a).split(", ",2)[1].split("]")[0].split(", ");

- 13,577
- 18
- 69
- 108
-
2Please, someone with sufficient reputation downvote this answer - it is exactly what it says it is - ugly! No intention to be rude, but in the interests of codeability please don't post this kind of thing! – Hack5 Mar 24 '17 at 16:52
-
if you are already using Arrays, it would be better to use Arrays.copyOfRange – Bishal Gautam Aug 07 '17 at 06:47
-
-
-
-
1