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 Overflow
Top answer
1 of 7
196

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
2 of 7
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.

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-remove-array-elements
How to Remove Array Elements in Java | DigitalOcean
May 2, 2025 - To remove an element from an array by index in Java, you need to create a new array with the desired size, copy elements before the index to be removed to the new array, and then copy elements after the index to be removed to the new array, starting from the position of the element to be removed.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ remove the first element from a list
Remove the First Element from a List | Baeldung
January 8, 2024 - This is because ArrayList uses an array under the hood, and the remove() operation requires copying the rest of the array to the beginning. The larger the array is, the more elements need to be shifted. Unlike that, LinkedList uses pointers meaning that each element points to the next and the previous one. Hence, removing the first element means just changing the pointer to the first element.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ why is it that removing an element from the start of an array o(n) while at the end o(1)?
r/learnprogramming on Reddit: Why is it that removing an element from the start of an array O(n) while at the end O(1)?
September 16, 2023 -

From my understanding the reason why removing the last element is O(1) is because you don't need to shift the array in memory. You simply remove the last element and leave the old space empty. So why is it that if you remove the first element that the Array HAS to to shift in memory (making it O(n))?

I don't understand the reasoning, if we are okay with leaving empty space in memory at the end of an array and not shifting all the other things surrounding the array in memory. Then why do we have to shift the array in memory if there is space that the start?

I am not understanding, if it's because the memory is trying to stay compact and no empty spaces are allowed. Then why don't all the other stuff in memory be shifted to the left after new space was cleared once we removed the last element from the array?

๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-remove-first-element-of-array
Remove First Element of Array in Java
October 16, 2021 - In the following example, we create a new array with the first element removed from the original array using Array.copyOfRange() method. ... import java.util.Arrays; public class Main { public static void main(String[] args) { int arr[] = {2, ...
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-you-remove-the-first-and-last-elements-from-a-Java-List
How to remove the first and last elements from a Java List - Quora
Answer (1 of 2): Youโ€™re not specifying the list type to be used. The most efficient way to remove the head and tail nodes of a list depends on the concrete list. The general solution (ignoring bound checks for now) is to do the following: [code]List list = ...; list.remove(0); list.remov...
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ remove/delete an element from an array in java
Remove/Delete An Element From An Array In Java
April 1, 2025 - To remove an element from an array, we first convert the array to an ArrayList and then use the โ€˜removeโ€™ method of ArrayList to remove the element at a particular index. Once removed, we convert the ArrayList back to the array. The following implementation shows removing the element from an array using ArrayList. import java.util.*; import java.util.stream.*; class Main { public static int[] remove_Element(int[] myArray, int index) { if (myArray == null || index &lt; 0 || index &gt;= myArray.length) { System.out.println("non-existing index"); return myArray; } //array to arrayList List&lt;
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 528600 โ€บ java โ€บ Remove-element-array-index
Remove an element from array by index (Beginning Java forum at Coderanch)
LinkedList = 136,060,386 ArrayList = 19,373,657 LinkedList = 89,518,985 ArrayList = 18,704,275 ... LinkedLists are great when: - adding to the start or the end - removing from the start or the end - adding, removing or setting using a ListIterator All direct element accesses will require the list to be traversed until that element is found, and that's what makes it slow if you use any indexed method.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ remove-element-from-an-array-in-java
Remove Element from an Array in Java
December 16, 2021 - Here, we're simply iterating over the original array and copying elements from the original array into the new array, skipping the one we'd like to remove. ... In case you're already using the Apache Commons library, you can use the ArrayUtils.remove() method.
๐ŸŒ
Java67
java67.com โ€บ 2012 โ€บ 12 โ€บ how-to-remove-element-from-array-in-java-example.html
How to Remove an Element from Array in Java with Example | Java67
Btw, if you are not familiar with array data structure itself, like it stores elements in a contiguous memory location, provides fast search capability in O(1) time if you know the index and adding and removing elements is very difficult then I also suggest you go through a comprehensive data structure and algorithm course like Data Structures and Algorithms: Deep Dive Using Java on Udemy. It's one of the most important topics and you just cannot afford to ignore it. This is also your first step towards becoming the better computer Programmer you always wanted to be. Anyway, here is our Java program to delete an array from a primitive array in Java:
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 496498 โ€บ remove-the-first-element-of-an-array
Remove the first element of an array | Sololearn: Learn to code for FREE!
Quick view: var empArr=[]; var maxArr = 50; var s; for(var i=0; i<maxArr; i++) { empArr.push(Math.round(Math.random() * 50)); document.write("Element " + i + ": " + empArr[i] + "<br>"); } document.write("<br>"); arr(); function arr(){ if(empArr.length>=50){ empArr.shift(); } for(var i=0; i<empArr.length; i++) { document.write("NEW Element " + i + ": " + empArr[i] + "<br>"); } } ... int arr = []; if(arr.length>=2500){ arr.splice(0,1); } 0 is the index from which you have to remove the element and 1 is the amount of elements to remove .
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ collection โ€บ java-collection-priority-queue-exercise-9.php
Java - Retrieve and remove the first element
import java.util.PriorityQueue; public class Exercise9 { public static void main(String[] args) { // Create Priority Queue PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>(); PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(); // Add numbers in the Priority Queue pq1.add(10); pq1.add(22); pq1.add(36); pq1.add(25); pq1.add(16); pq1.add(70); pq1.add(82); pq1.add(89); pq1.add(14); System.out.println("Original Priority Queue: "+pq1); System.out.println("Removes the first element: "+pq1.poll()); System.out.println("Priority Queue after removing first element: "+pq1); } }
๐ŸŒ
Quora
quora.com โ€บ How-can-I-write-a-program-to-delete-a-first-element-of-an-array
How to write a program to delete a first element of an array - Quora
Answer (1 of 7): Start shifting elements from second position, i.e.a[1] till a[n-1], an index behind. When you overwrite a[0] with a[1],it automatically gets deleted. [code c] for(i=1;i