How to delete the content of an array of objects. If there is other ways to delete a content of an array of objects , please do share.
import java.util.Arrays;
import java.util.Scanner;
import org.apache.commons.lang3.ArrayUtils;
public class Testing { public static void deleteItem(ItemTracker[] listItems) { System.out.println("Which item you want to delete? "); for(int i=0; i < listItems.length; i++) { if(input.equalsIgnoreCase("Quantity")) { // Some Code } else if(input.equalsIgnoreCase("Something"){ ArrayUtils.remove(listItems, i); // This is the part where it should delete .. but it doesnt delete. } break; } } } 2 6 Answers
Change this
ArrayUtils.remove(listItems, i);to
listItems = ArrayUtils.remove(listItems, i);As you can see in the JavaDoc, the method does not change the argument listItems, rather it returns a new array with the remaining elements.
Edit
You also need to change your deletion method to
public static ItemTracker[] deleteItem(ItemTracker[] listItems) { //..
}So you could return the new array with the remaining elements.
16Store the resulting array.
It won't change the original array object.
listItems = ArrayUtils.remove(listItems, i);Edit: But for using this method you need the change to return type of your method
public static ItemTracker[] deleteItem(ItemTracker[] listItems){ System.out.println("Which item you want to delete? "); for(int i=0; i < listItems.length; i++) { if(input.equalsIgnoreCase("Quantity")) { // Some Code } else if(input.equalsIgnoreCase("Something"){ listItems = ArrayUtils.remove(listItems, i); // This is the part where it should delete .. but it doesnt delete. } break; } return listItems; } 18 In your case usage of ArrayUtils is incorrect and redundant. You can delete element in next way:
// ...
listItems[i] = null;
// array will looks like [o1, o2, null, o3, o4, ...]
// ... There is no other way without changing method's return type
4Without additional libraries, with temporary list:
Element arrayToRemoveFrom[];
Element toRemove; // should be known already
ArrayList<Element> tmpList = new ArrayList<Element>(Arrays.asList(arrayToRemoveFrom));
tmpList.remove(toRemove);
// any other code processing and removing elements
arrayToRemoveFrom = tmpList.toArray(new Arrays[tmlList.size()]); This method returns a new array with the same elements of the input array except the element on the specified position. The component type of the returned array is always the same as that of the input array.
So,you should use it like this
listItems = ArrayUtils.remove(listItems, i);NOTE
- Here we have assign the returned array to current
listItem. - As this method does not change the actual array but returns the changed array after removal same as
#replacemethod works forString.
2YES. I agree with
zvdhI have missed the purpose of your method because I was more concentrated on removal of element.Sorry for that!! as this will not actually change thelistItemand you need toreturnthe new array which contains the change.
class Arrays
{ public static void main(String[] args) { double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; java.util.Arrays.sort(numbers); System.out.print("Ascending order= "); for (int i = 0; i < numbers.length; i++) System.out.print(numbers[i] + " "); System.out.println(); System.out.print("Decending order= "); for (int i = numbers.length -1; i >= 0; i--) System.out.print(numbers[i] + " "); }
}This solution only displays in reverse order, but it can be changed to reorder the array using the same loop.