You could use commons lang's ArrayUtils.

array = ArrayUtils.removeElement(array, element)

commons.apache.org library:Javadocs

Answer from Peter Lawrey on Stack Overflow
🌐
Edureka Community
edureka.co › home › community › categories › java › how can we remove an element from an array in...
How can we remove an element from an array in Java | Edureka Community
May 21, 2018 - How can we remove an element from an array in... how do I turn a double in an array from 1000 to infinity Jul 9, 2024 · I have created a code in java for a flower shop and now my IDE is showing all the inputs are in red showing there is an error Jul 6, 2024
Discussions

java - Function to remove element from an array - Code Review Stack Exchange
Below function takes an array, an element to be deleted and returns a new array minus the indicated element. Can this be optimized some how? How good is it generally? public static int[] sbSample... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
December 7, 2015
Removing an item from an array
Hello sun forum, I wonder if anyone would be kind enough to assist a newbie Java hobbyist with a few basic questions? I don't want to take up too much of your time so I will be brief. I want to pro... More on forums.oracle.com
🌐 forums.oracle.com
November 17, 2007
Removing an item from an array list
The problem is in this part: for (Student i : roster) { int sId = i.getId(); if (id == sId) { roster.remove(id); } else if (id != sId) { System.out.println("ERROR: Invalid student ID.\n"); } } You can't remove items from the list while you're looping through it with this for loop. If you want to use a loop, you should use this kind of loop: for (int i = 0; i < roster.size(); ++i) { Student s = roster.get(i); int sId = s.getId(); if (id == sId) { roster.remove(i); i--; // This is important! } } See, this lets you remove the student at the current index. However, as you remove it, you have to decrement the index, otherwise you'll skip the next item in the list. More on reddit.com
🌐 r/learnjava
8
5
March 31, 2017
Having trouble with java arrays, removing an element then returning a new size of an array
I’m on my phone so I’ll just explain what im thinking in pseudo code, but why don’t you just do something like: Create a new array of size n-1 where n is the size of the original array. Iterate over the old array in a for loop, adding each element to the new array. If the element at the ith position equals the element to remove, continue to the next iteration. Would that work? EDIT: I'm at my PC now so here's the code. I ended up doing it a little differently than I mentioned above. Note that it doesn't account for if the element appears multiple times in the array or when the element does not exist. public static void main(String []args) { int[] myArray = { 5,6,9,4 }; int[] newArray = removeElem(myArray, 9); for (int i = 0; i < newArray.length; i++) System.out.print(newArray[i] + " "); } public static int[] removeElem(int[] array, int value) { int index = -1; // First confirm the element to remove exists in the array for (int i = 0; i < array.length; i++) { // If it does, store its index if (array[i] == value) index = i; } // If element exists if (index != -1) { int[] newArray = new int[array.length - 1]; for (int i = 0; i < array.length; i ++) { if (i < index) newArray[i] = array[i]; else if (i > index) newArray[i - 1] = array[i]; } return newArray; } // Do some error handling for if the element does not exist else return array; } Output is: 5 6 4 More on reddit.com
🌐 r/learnprogramming
17
4
July 13, 2018
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › removeAll
Java ArrayList removeAll() - Remove Specified Elements | Vultr Docs
September 27, 2024 - The output will indicate whether elements were successfully removed from originalList. Sometimes, you want to remove elements based on more complex conditions rather than just direct matches. Java 8 introduced Predicate and stream features that can be used to handle such cases more flexibly. Use a stream to filter and collect elements that should remain. Replace the contents of the original list. ... ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6)); numbers.removeIf(n -> n % 2 == 0); // Remove all even numbers System.out.println(numbers); Explain Code
🌐
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 - Using Java8 streams, we can delete an element from an array. In order to do this, first, the array is converted to a stream. Then the element at the specified index is deleted using the filter method of streams.
🌐
Java Concept Of The Day
javaconceptoftheday.com › home › array element removal programs in java
Array Element Removal Programs In Java
October 22, 2018 - But, there are no methods to remove elements from an array in java.util.Arrays class. There are some third party libraries which can be used to remove elements from arrays in java. In this post, I have demonstrated how to remove one or more elements from an existing array using third party library org.apache.commons.lang3.ArrayUtils class.
🌐
Java Guides
javaguides.net › 2024 › 05 › java-remove-element-from-array.html
Java: Remove Element from Array
May 29, 2024 - This guide will cover different ways to remove an element from an array, including using loops, the System.arraycopy method, and converting the array to a list and back to an array. ... In Java, arrays are fixed-size data structures that store elements of the same type.
Top answer
1 of 2
3

The methodname sbSample doesn't tell anything about what the method is doing and this will make debugging the code very hard if not impossible.

You should leave your methodparameters some space to breathe so int a[],int i should be int a[], int i. The same is true for variables and operators.

Comments should tell the reader of the code why something is done in the way it is done. Stating the obvious what is done is only adding noise to the code and should be removed. Let the code itself tell what is done by using meaningful names for classes, methods and variables.

This method is doing to much hence it is breaking the single responsibility principle. It is "deleting" array elements and displaying the resulting array values.

If the source array has 10 elements and 3 of them have the value which you want to delete, the resulting array still has 10 elements which is somehow strange. Deleting means IMO that the size of the array should shrink afterwards. How about using an ArrayList<T> adding the items which aren't satisfying the search condition and later call toArray() to return the resulting array ?

2 of 2
1
  1. I recommend to do the System.out.println in a separate method (or simply use Arrays.toString(..)
  2. I recommend to use != instead of !(....=)...)
  3. I recommend to return an array with the size matching the new number of entries.
  4. even if a.length might be cheap to compute, you should avoid computing the same over and over again in the loop-condition.

    public static int[] sbSample(int a[], int i) {
        // Idea: store the not deleted elements in a temporary array
        // and at the end create on with the fitting size and return it.
        int len = a.length;
        int size = 0;
        int temp[] = new int[len];
        for (int k = 0; k < len; k++) {
            if (a[k] != i) {
                temp[size++] = a[k];
            }
        }
        int[] result = new int[size];
        System.arraycopy(temp, 0, result, 0, size);
        return result;
    }
    
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › home › java_data_structures › remove elements from array in java
Remove Elements from Array in Java
September 1, 2008 - Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA. Step 1 - Start Step 2 - Set J = K Step 3 - Repeat steps 4 and 5 while J < N Step 4 - Set LA[J] = LA[J + 1] Step 5 - Set J = J+1 Step 6 - Set N = N-1 Step 7 - Stop · public class RemovingElements { public static void main(String args[]) { int[] myArray = {10, 20, 30, 45, 96, 66}; int pos = 3; int j = myArray.length; for(int i = pos; i < j-1; i++) { myArray[i] = myArray[i+1]; } System.out.println("Contents of the array after deletion ::"); for(int i = 0; i < myArray.length-1; i++) { System.out.print(myArray[i]+ ", "); } } }
🌐
GitHub
github.com › kunal-kushwaha › DSA-Bootcamp-Java › blob › main › assignments › 05-arrays.md
DSA-Bootcamp-Java/assignments/05-arrays.md at main · kunal-kushwaha/DSA-Bootcamp-Java
Remove Duplicates from Sorted Array · Minimum Cost to Move Chips to The Same Position · Spiral Matrix · Spiral Matrix II · Spiral Matrix III · Set Matrix Zeroes · Product of Array Except Self · Find First and Last Position of Element in Sorted Array ·
Author   kunal-kushwaha
🌐
Programiz
programiz.com › java-programming › library › arraylist › remove
Java ArrayList remove()
Become a certified Java programmer. Try Programiz PRO! ... The remove() method removes the single element from the arraylist.
🌐
Netjstech
netjstech.com › 2017 › 07 › how-to-remove-elements-from-array-java.html
How to Remove Elements From an Array Java Program | Tech Tutorials
When you remove an element from an array, you can fill the empty space with 0, space or null depending on whether it is a primitive array, string array or an Object array. Other alternative is to create a new array and copy the elements in that array. New array should have size of old array’s ...
🌐
Baeldung
baeldung.com › home › java › java array › removing the first element of an array
Removing the First Element of an Array | Baeldung
April 22, 2025 - First of all, removing an element of an array isn’t technically possible in Java.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-list-remove-methods-arraylist-remove
How To Use remove() Methods for Java List and ArrayList | DigitalOcean
September 9, 2025 - You can use the remove() method to achieve this. Here’s an example: List<Post> posts = new ArrayList<>(); // Assuming posts is populated with user posts // Remove all posts from a banned user String bannedUserId = "bannedUser123"; posts.removeIf(post -> post.getUserId().equals(bannedUserId));
🌐
log2base2
log2base2.com › data-structures › array › remove-a-specific-element-from-array.html
Remove a specific element from array
/* * Program : Remove an element in the array * Language : C */ #include<stdio.h> #define size 5 int main() { int arr[size] = {1, 20, 5, 78, 30}; int key, i, index = -1; printf("Enter element to delete\n"); scanf("%d",&key); /* * iterate the array elements using loop * if any element matches the key, store the index */ for(i = 0; i < size; i++) { if(arr[i] == key) { index = i; break; } } if(index != -1) { //shift all the element from index+1 by one position to the left for(i = index; i < size - 1; i++) arr[i] = arr[i+1]; printf("New Array : "); for(i = 0; i < size - 1; i++) printf("%d ",arr[i]); } else printf("Element Not Found\n"); return 0; } Run it
🌐
Medium
medium.com › @saraswathirajkumar18 › how-to-remove-or-delete-element-from-array-using-java-e74906403092
How to remove (or) delete element from array using java | by Saraswathi Rajkumar | Medium
March 28, 2024 - import java.util.Arrays; import java.util.Scanner; public class RemoveElementinArray { //using another array variable //method with parameter of array and element we want to remove public static void removeElement(int[] arr,int element) { //create ...
🌐
Codeforces
codeforces.com › blog › entry › 151767
The 1 or n Trick – Understanding Deletion Sort - Codeforces
3 days ago - But the game stops immediately when the array becomes non-decreasing. This leads to only two possible outcomes. ... Then the game stops immediately. We cannot delete anything. ... Since we can remove any element, we can keep deleting elements until only one remains. A single element is always sorted. ... That’s it. No DP. No LIS. No greedy complexity. Just observation. import java.util.*; public class B_DeletionSort { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } boolean ok = true; for (int i = 0; i < n ; i++) { if (a[i] > a[i + 1]) { ok = false; break; } } if (ok) { System.out.println(n); } else { System.out.println(1); } } } }
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › splice
Array.prototype.splice() - JavaScript | MDN
If deleteCount is omitted, or if its value is greater than or equal to the number of elements after the position specified by start, then all the elements from start to the end of the array will be deleted. However, if you wish to pass any itemN parameter, you should pass Infinity as deleteCount to delete all elements after start, because an explicit undefined gets converted to 0. If deleteCount is 0 or negative, no elements are removed.
🌐
Oracle
forums.oracle.com › ords › apexds › post › removing-an-item-from-an-array-7950
Removing an item from an array
November 17, 2007 - Hello sun forum, I wonder if anyone would be kind enough to assist a newbie Java hobbyist with a few basic questions? I don't want to take up too much of your time so I will be brief. I want to pro...
🌐
Coderanch
coderanch.com › t › 536091 › java › Remove-replace-array-element
Remove & replace array element (Beginning Java forum at Coderanch)
April 28, 2011 - Matthew Brown wrote:It sounds like what you really need is to not use an array. Instead, use a List (such as ArrayList or LinkedList). That's got a remove(int) method that will do exactly what I think you want - remove the object at that position from the list (moving all the following elements up one), and returning that object.
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › remove
Java ArrayList remove() - Delete Element
November 14, 2024 - Create an ArrayList with some elements. Use the remove() method with the object you want to delete. ... ArrayList<String> pets = new ArrayList<>(); pets.add("Dog"); pets.add("Cat"); pets.add("Bird"); pets.remove("Cat"); System.out.println(pets); ...