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 ?
Removing an element from an Array (Java) - Stack Overflow
Removing an item from an array
Removing an item from an array list
Having trouble with java arrays, removing an element then returning a new size of an array
Videos
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 ?
- I recommend to do the
System.out.printlnin a separate method (or simply useArrays.toString(..) - I recommend to use
!=instead of!(....=)...) - I recommend to return an array with the size matching the new number of entries.
even if
a.lengthmight 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; }
You could use commons lang's ArrayUtils.
array = ArrayUtils.removeElement(array, element)
commons.apache.org library:Javadocs
Your question isn't very clear. From your own answer, I can tell better what you are trying to do:
public static String[] removeElements(String[] input, String deleteMe) {
List result = new LinkedList();
for(String item : input)
if(!deleteMe.equals(item))
result.add(item);
return result.toArray(input);
}
NB: This is untested. Error checking is left as an exercise to the reader (I'd throw IllegalArgumentException if either input or deleteMe is null; an empty list on null list input doesn't make sense. Removing null Strings from the array might make sense, but I'll leave that as an exercise too; currently, it will throw an NPE when it tries to call equals on deleteMe if deleteMe is null.)
Choices I made here:
I used a LinkedList. Iteration should be just as fast, and you avoid any resizes, or allocating too big of a list if you end up deleting lots of elements. You could use an ArrayList, and set the initial size to the length of input. It likely wouldn't make much of a difference.