How can I delete an item from an array, and then resize the array to the smaller size? Likewise, how can I increase the capacity if I need to add another item?
-
is 'array' the required data-structure for your use case? Seems like you're using the wrong hammer – Ryan Fernandes Feb 02 '11 at 06:53
9 Answers
The size of a Java array is fixed when you allocate it, and cannot be changed.
If you want to "grow" or "shrink" an existing array, you have to allocate a new array of the appropriate size and copy the array elements; e.g. using
System.arraycopy(...)
orArrays.copyOf(...)
. A copy loop works as well, though it looks a bit clunky ... IMO.If you want to "delete" an item or items from an array (in the true sense ... not just replacing them with
null
), you need to allocate a new smaller array and copy across the elements you want to retain.Finally, you can "erase" an element in an array of a reference type by assigning
null
to it. But this introduces new problems:- If you were using
null
elements to mean something, you can't do this. - All of the code that uses the array now has to deal with the possibility of a
null
element in the appropriate fashion. More complexity and potential for bugs1.
- If you were using
There are alternatives in the form of 3rd-party libraries (e.g. Apache Commons ArrayUtils
), but you may want to consider whether it is worth adding a library dependency just for the sake of a method that you could implement yourself with 5-10 lines of code.
It is better (i.e. simpler ... and in many cases, more efficient2) to use a List
class instead of an array. This will take care of (at least) growing the backing storage. And there are operations that take care of inserting and deleting elements anywhere in the list.
For instance, the ArrayList
class uses an array as backing, and automatically grows the array as required. It does not automatically reduce the size of the backing array, but you can tell it to do this using the trimToSize()
method; e.g.
ArrayList l = ...
l.remove(21);
l.trimToSize(); // Only do this if you really have to.
1 - But note that the explicit if (a[e] == null)
checks themselves are likely to be "free", since they can be combined with the implicit null
check that happens when you dereference the value of a[e]
.
2 - I say it is "more efficient in many cases" because ArrayList
uses a simple "double the size" strategy when it needs to grow the backing array. This means that if grow the list by repeatedly appending to it, each element will be copied on average one extra time. By contrast, if you did this with an array you would end up copying each array element close to N/2 times on average.

- 698,415
- 94
- 811
- 1,216
You can't resize the array, per se, but you can create a new array and efficiently copy the elements from the old array to the new array using some utility function like this:
public static int[] removeElement(int[] original, int element){
int[] n = new int[original.length - 1];
System.arraycopy(original, 0, n, 0, element );
System.arraycopy(original, element+1, n, element, original.length - element-1);
return n;
}
A better approach, however, would be to use an ArrayList (or similar List structure) to store your data and then use its methods to remove elements as needed.

- 29,538
- 35
- 110
- 138

- 75,278
- 22
- 140
- 160
-
1There is a bug in this code, which will cause it to always throw a NPE. I have edited answer with fix. – Joel Aug 15 '12 at 09:49
-
Using ArrayUtils.removeElement(Object[],Object)
from org.apache.commons.lang is by far the easiest way to do this.
int[] numbers = {1,2,3,4,5,6,7};
//removing number 1
numbers =(int[])ArrayUtils.removeElement(numbers, 1);

- 752
- 5
- 9
-
2This does not really resize the array. It creates a new one. But there is no other way, because the size of arrays cannot be changed. – MrSmith42 Jan 23 '13 at 16:17
Since an array has a fixed size that is allocated when created, your only option is to create a new array without the element you want to remove.
If the element you want to remove is the last array item, this becomes easy to implement using Arrays.copy
:
int a[] = { 1, 2, 3};
a = Arrays.copyOf(a, 2);
After running the above code, a will now point to a new array containing only 1, 2.
Otherwise if the element you want to delete is not the last one, you need to create a new array at size-1 and copy all the items to it except the one you want to delete.
The approach above is not efficient. If you need to manage a mutable list of items in memory, better use a List. Specifically LinkedList
will remove an item from the list in O(1)
(fastest theoretically possible).

- 12,250
- 6
- 53
- 70
-
For me the best and shortest working solution to truncate an Array. Thanks! – Ruwen Sep 26 '17 at 12:54
I have created this function, or class. Im kinda new but my friend needed this also so I created this:
public String[] name(int index, String[] z ){
if(index > z.length){
return z;
} else {
String[] returnThis = new String[z.length - 1];
int newIndex = 0;
for(int i = 0; i < z.length; i++){
if(i != index){
returnThis[newIndex] = z[i];
newIndex++;
}
}
return returnThis;
}
}
Since its pretty revelant, I thought I would post it here.

- 19
- 1
-
No. Not relevant. It does not resize the original array. (And besides, there are simpler ways to do this.) – Stephen C Mar 11 '20 at 23:59
Arrays are fixed in size, you cannot resize them after creating them. You can remove an existing item by setting it to null
:
objects[4] = null;
But you won't be able to delete that entire slot off the array and reduce its size by 1.
If you need a dynamically-sized array, you can use an ArrayList
. With it, you can add()
and remove()
objects, and it will grow and shrink as needed.

- 700,868
- 160
- 1,392
- 1,356
-
-
@Joe: That's also a possible option; take a look at the other answers. – BoltClock Feb 02 '11 at 02:33
object[] newarray = new object[oldarray.Length-1];
for(int x=0; x < array.Length; x++)
{
if(!(array[x] == value_of_array_to_delete))
// if(!(x == array_index_to_delete))
{
newarray[x] = oldarray[x];
}
}
There is no way to downsize an array after it is created, but you can copy the contents to another array of a lesser size.

- 1,412
- 16
- 24
-
-
Then your instantiation would be `object[] new array = new object[oldarray.Length-2];` the for loop takes care of the increments. – Mike Aug 21 '12 at 13:48
-
I means.. Do we need to create a new Array even if we just want to ignore one byte.. Can't we shrink the Array ? – Amit Aug 22 '12 at 09:32
-
No, you need to declare a new array and copy it's contents, if you want to shrink the array. – Mike Aug 22 '12 at 17:53
without using the System.arraycopy method you can delete an element from an array with the following
int i = 0;
int x = 0;
while(i < oldArray.length){
if(oldArray[i] == 3)i++;
intArray[x] = oldArray[i];
i++;
x++;
}
where 3 is the value you want to remove.

- 423
- 5
- 16
No use of any pre defined function as well as efficient: --- >>
public static void Delete(int d , int[] array )
{
Scanner in = new Scanner (System.in);
int i , size = array.length;
System.out.println("ENTER THE VALUE TO DELETE? ");
d = in.nextInt();
for ( i=0;i< size;i++)
{
if (array[i] == d)
{
int[] arr3 =new int[size-1];
int[] arr4 = new int[i];
int[] arr5 = new int[size-i-1];
for (int a =0 ;a<i;a++)
{
arr4[a]=array[a];
arr3[a] = arr4[a];
}
for (int a =i ;a<size-1;a++)
{
arr5[a-i] = array[a+1];
arr3[a] = arr5[a-i];
}
System.out.println(Arrays.toString(arr3));
}
else System.out.println("************");
}
}

- 2,838
- 3
- 32
- 47

- 1
- 1