How do I return an object that was deleted?

I have a method that is supposed to remove an InventoryItem (i) in an array list (iList) when the description part of that InventoryItem is entered. The method should return the item that was removed and I'm not sure how to do this. Here is 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;


}

      

+3


source to share


5 answers


I cannot use an iterator. [...] I need to use a for every 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 just wanted to find and return an object instead of deleting it, [...]

Then you just skip the line iList.remove(item)

.

But it would be better to split the methods like this:

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;
}

      

+4


source


You need to save the object before deleting it. Then return this object.

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) {
        return iList.remove(index);
     } 
  }
  return null;
}

      

NOTE if this return null means nothing has been removed.




EDIT

If you just want to return the object and not delete it. Replace return iList.remove(index);

withreturn iList.get(index);

+2


source


It seems to me you just need a keyword return

in front of your method remove

. But considering you messed this up and it would throw a ConcurrentModificationException, I changed it a bit.

public InventoryItem deleteInventoryItem(String descriptionIn) {
  Iterator<InventoryItem> iterator = iList.iterator();
  while(iterator.hasNext())
  {
      InventoryItem i = iterator.next();
      if (i.getDescription().toLowerCase().startsWith(descriptionIn.toLowerCase())) {
          iterator.remove();
          return i;
      }
  }
  return null;
}

      

0


source


You are simply removing it from the list, not removing the object from memory. You already have a link to it, so you can simply return it after removing it from the list.

for (InventoryItem i : iList) {
    if (i.getDescription().toLowerCase().startsWith(descriptionIn.toLowerCase())) {
        iList.remove(i);
        return i;
    }
}

      

0


source


Something like that. I just edited it to avoid ConcurrentModificationException and also moved the index declaration into a for loop.

public InventoryItem deleteInventoryItem(String descriptionIn) {
  InventoryItem returnItem = null;
  InventoryItem i = null;
  for (int j = 0; j < iList.size(); j++) {
       boolean result = false;
       i = iList.get(j);
       if (i.getDescription().toLowerCase().startsWith(descriptionIn.toLowerCase())) {
           returnItem = i;
           iList.remove(j);  
           j--;
       } 
   }
   return returnItem;
}

      

0


source







All Articles