How to return an object that was deleted?

kb94

I have a method that is supposed to delete an InventoryItem (i) in an array list (iList) when part of the description of that InventoryItem is entered. The method has to return the item that was deleted, and I'm unsure of how to do this. Here's my code so far.

public InventoryItem deleteInventoryItem(String descriptionIn) {

  int index = -1;
  boolean result = false;

  for (InventoryItem i : iList) {
     if (i.getDescription().toLowerCase().startsWith(descriptionIn.toLowerCase())) {
        index = (iList.indexOf(i));
        result = true;  
     } 

     if (index >= 0) {
        iList.remove(index);
     } 
  }

  return null;


}
aioobe

I can't use an iterator. [...] I need to use a for each loop

Here's one way:

public InventoryItem deleteInventoryItem(String descriptionIn) {
    for (InventoryItem item : iList)
        if (item.getDescription()
                .toLowerCase()
                .startsWith(descriptionIn.toLowerCase())) {
            iList.remove(item);
            return item;
        }
    }
    return null;
}

Note that this removes at most one object from the list. If there are multiple matches, only the first match will be removed.

If I simply wanted to find and return an object instead of deleting it, [...]

Then you simply skip the iList.remove(item) row.

But even better would be to split up the methods as follows:

public InventoryItem findInventoryItem(String descriptionIn) {
    for (InventoryItem item : iList)
        if (item.getDescription()
                .toLowerCase()
                .startsWith(descriptionIn.toLowerCase())) {
            return item;
        }
    }
    return null;
}

public InventoryItem deleteInventoryItem(String description) {
    InventoryItem item = findInventoryItem(description);
    if (item != null)
        iList.remove(item);
    return item;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return object literal in JavaScript

From Dev

Redips - how to get object before event.deleted handler

From Dev

How to return object method properly

From Dev

How to return mock object as a null

From Dev

How to make object return generic

From Dev

How to return an object with no copy constructor

From Dev

How to return the sum in an object ArrayList

From Dev

How to return an object in my case?

From Dev

How to return HashMap object fields

From Dev

How to return an object with multiple types

From Dev

How to get Express to return the object I just deleted in MongoDB

From Dev

How to return a valid JSON object?

From Dev

How to delete from list, return the deleted element, and return the "modified" list

From Dev

How to return object of multiple classes?

From Dev

How to return object?

From Dev

How to update an object or bail if it has been deleted in Django

From Dev

How to return the position of object by names?

From Dev

How to search and return json object?

From Dev

How to return an object inside an array?

From Dev

How to make object return generic

From Dev

How to return object?

From Dev

How to update an object or bail if it has been deleted in Django

From Dev

How to return a List<object> in WCF

From Dev

How to return Object from ComboBox

From Dev

how to add list of object and return it

From Dev

How to return an object created in a function?

From Dev

How to return a value or an object in Eiffel?

From Dev

How to return an object in powershell

From Dev

Java return a deleted file