Removing an element from an Array (Java)
You could use commons lang's ArrayUtils.
array = ArrayUtils.removeElement(array, element)
commons.apache.org library:Javadocs
How do you delete an element from an array without creating a new array or using ArrayLists? java
There is no direct way to remove elements from an Array in Java. Though Array in Java objects, it doesn't provide any methods to add()
, remove()
or search an element in Array. This is the reason Collection classes like ArrayList
and HashSet are very popular.
Though
Thanks to Apache Commons Utils, You can use there
ArrayUtils
class
to remove an element from array more easily than by doing it yourself.
One thing to remember is that Arrays are fixed size in Java, once you
create an array you can not change their size, which means removing or
deleting an item doesn't reduce the size of the array. This is, in
fact, the main difference between Array and ArrayList in Java.
What you need to do is create a new array and copy the remaining content of this array into a new array using System.arrayCopy()
or any other means. In fact, all other API and functions you will use do this but then you don't need to reinvent the wheel.
import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;
....
//let's create an array for demonstration purpose
int[] test = new int[] { 101, 102, 103, 104, 105};
System.out.println("Original Array : size : "
+ test.length );
System.out.println("Contents : " + Arrays.toString(test));
// let's remove or delete an element from an Array
// using Apache Commons ArrayUtils
test = ArrayUtils.remove(test, 2); //removing element at index 2
// Size of an array must be 1 less than the original array
// after deleting an element
System.out.println("Size of the array after
removing an element : " + test.length);
System.out.println("Content of Array after
removing an object : " + Arrays.toString(test));
Remove an element from array and replace with 0?
Here is an approach, similar to what I described in the comments above.
import java.util.Arrays;
public class main
{
// tip: arguments are passed via the field below this editor
public static void main(String[] args)
{
int[] arr = {3, 5, 7, 8, 5, 12, 2};
remove(arr, 5);
System.out.println(Arrays.toString(arr)); // [3, 7, 8, 5, 12, 2, 0]
}
public static void remove(int[] arr, int toRemove) {
int idx = -1;
// determine first occurrence of toRemove
for(int i = 0; i < arr.length; i++) {
if(arr[i] == toRemove) {
idx = i;
break;
}
}
// if not found, return
if(idx == -1) return;
// shift other elements down
for(int i = idx; i < arr.length-1; i++) {
arr[i] = arr[i+1];
}
// set last element to 0
arr[arr.length-1] = 0;
}
}
Removing elements from an array in Java
Why not create a variable to store whether you have found the candidate or not? Then, if the variable says that you have not found it, the second loop can be avoided. For example,
...
boolean foundCandidate = false; // initialized to "did not find it"
for( int i = 0; i < carRegister.length; i++ ) {
String s = carRegister[i];
if( s.equals( candidate ) ) {
// changed if found
foundCandidate = true;
...
// now, you can avoid this loop (and modifying carRegister) if it is not necessary
if( foundCanidate) {
...
}
Removing the first element in an array and returning it
Perhaps you missed to maintain counter on removal as in
public T removeItem(){
int n = array.length;
T t = array[0];
if(empty())
return null;
for(int i = 0; i < n -1; i++) {
array[i] = array[i + 1];
}
numberOfItems--; // Missed piece
return t;
}
Also, the description is inaccurate, your code(as is) repeatedly returns the last element and not the first one.
Related Topics
Implementing Two Interfaces in a Class with Same Method. Which Interface Method Is Overridden
Remove Diacritical Marks (Ń Ǹ Ň ñ Ṅ Ņ Ṇ Ṋ Ṉ ̈ Ɲ Ƞ ᶇ ɳ ȵ) from Unicode Chars
Why Does Inetaddress.Isreachable Return False, When I Can Ping the Ip Address
How to Use a Custom Serializer with Jackson
Initialising a Multidimensional Array in Java
Appending Files to a Zip File with Java
How to Test If a Double Is an Integer
Split String into Array of Character Strings
How to Set the Classpath in Netbeans
How to Import a .Cer Certificate into a Java Keystore
Java, Classpath, Classloading => Multiple Versions of the Same Jar/Project