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
Answer from jjnguy on Stack OverflowThe 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
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.
If the comparison for "remove first" is between the ArrayList and the LinkedList classes, the LinkedList wins clearly.
Removing an element from a linked list costs O(1), while doing so for an array (array list) costs O(n).
I think that what you need is an ArrayDeque (an unfairly overlooked class in java.util). Its removeFirst method performs in O(1) as for LinkedList, while it generally shows the better space and time characteristics of ArrayList. It’s implemented as a circular queue in an array.
You should very rarely use LinkedList. I did once in my 17 years as Java programmer and regretted in retrospect.
Stream the list for a match, then feed the match to the list's remove() method which will remove the first occurrence.
list.stream()
.filter(e -> e.getId().equals("FOO")) // Condition here
.findFirst()
.ifPresent(list::remove);
That's the "look at me, I know streams" version. For the most efficient uselessly micro-optimized version you would use Iterator.
Iterator<Foo> itr = list.iterator();
while(itr.hasNext()) {
if(itr.next().getId().equals("FOO")) {
itr.remove();
break;
}
}
Here's an iterator option:
for (Iterator<T> iterator = list.iterator();
current = iterator.next() ;
iterator.hasNext()) {
if (yourPredicate.test(element)) {
iterator.remove();
break;
}
}
create subList()
Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.
Check implementation of this method and make some tests to determine performance
Jigar Joshi's answer is already contains the solution you need. I wanted to add some other stuff. Calling clear() on the sublist will handle your work, I guess. But it might be using
iteration in the background, I'm not sure. Example script for your use:
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> subList = (ArrayList<Integer>) list.subList(0, 9);
subList.clear();