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 ?

Answer from Heslacher on Stack Exchange
🌐
DEV Community
dev.to › a_b_102931 › removing-an-element-in-an-array-in-place-2hb7
Removing an Element in an Array In-Place - DEV Community
June 9, 2020 - In this problem, we'll want to remove elements at certain indexes, which means we'll pass in two parameters into .splice -- the first is the index of the value we want to remove, and the second is the number 1, since we only want to remove one value at a time. The other important thing to plan for when modifying arrays in place is that you'll have to account for removed elements when you're traversing through the array.
🌐
Reddit
reddit.com › r › AskProgramming › comments › krzep7 › java_remove_element_from_array
Java - Remove element from array? : r/AskProgramming
March 7, 2020 - public void removeDog(Dog d) { Dog[] copyOfArr = Arrays.copyOf(dogsOwned, dogsOwned.length - 1); for (int i = 0; i < dogsOwned.length; i++) { if (!dogsOwned[i].equals(d)) { copyOfArr[i] = dogsOwned[i]; dogsOwned = copyOfArr; } } }
Discussions

Removing an element from an Array (Java) - Stack Overflow
Is there any fast (and nice looking) way to remove an element from an array in Java? More on stackoverflow.com
🌐 stackoverflow.com
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
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;
    }
    
🌐
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 ...
🌐
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.
🌐
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));
Find elsewhere
🌐
Stack Abuse
stackabuse.com › remove-element-from-an-array-in-java
Remove Element from an Array in Java
December 16, 2021 - The final argument is the number of elements to copy from the source array. The arraycopy is generally used to copy contents from some source array into some destination array. Though, we can also copy an array, or a part of it, into itself. This allows us to shift a part of it to the left like last time: int[] array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int index = 3; To remove the element, we only need to write this one line of code:
🌐
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.
🌐
Quora
quora.com › How-does-one-remove-an-element-from-an-array-in-Java
How does one remove an element from an array in Java? - Quora
Answer (1 of 10): Removing an element from an array is a cumbersome effort if the order matters in your array. Otherwise, it is really easy. Let me explain both ways. Order of the elements does NOT matter Let’s assume your have a partially filled array [5, 9, 6, 8, 0, 0, 0] (0 means don’t ...
🌐
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]+ ", "); } } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-an-element-at-specific-index-from-an-array-in-java
Remove an Element at Specific Index from an Array in Java - GeeksforGeeks
July 11, 2025 - Arrays have a fixed size, so creating a new array without the target element is often necessary. ... The basic approach to remove an element at a specific index is using a loop. ... // Java program to remove an element // from a specific index ...
🌐
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.
🌐
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 ...
🌐
Asjava
asjava.com › home › how to remove an element from an array in java
How to Remove an Element From an Array Java: A Guide
March 20, 2024 - Using the same example as before, if we want to remove the element at index 2 (which is 30) from the array [10, 20, 30, 40, 50], we would get the same array with the following elements: [10, 20, 40, 50, 0]. Note that the last element has been set to 0, which indicates that it is no longer part of the array. ... The System class in Java provides a method called arraycopy() that allows us to copy elements from one array to another.
🌐
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.
🌐
W3Schools
w3schools.com › java › ref_arraylist_remove.asp
Java ArrayList remove() Method
If a value is specified and multiple elements in the list have the same value then only the first one is deleted. If the list contains integers and you want to delete an integer based on its value you will need to pass an Integer object. See More Examples below for an example. ... T refers to the data type of items in the list. Remove an integer from the list by position and by value: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(8); list.add(9); list.add(1); list.remove(Integer.valueOf(1)); // Remove by object list.remove(1); // Remove by index System.out.println(list); } }
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › arraylist removeif(): remove elements matching a condition
ArrayList removeIf(): Remove Elements Matching a Condition
August 7, 2023 - Java ArrayList.removeIf() method removes all elements that satisfy a condition by iterating through the elements of the current arraylist and matching them against the condition specified by the argument Predicate.