How to remove data from a linked list in java

I create a demo basket in Android for this, I use the application class to save data. I cannot remove data from a linked list. I call the removeItem () function for android activity to remove the selected item from the list, but it does not work, can someone help me.

package in.co.santoshsharma.bookshopping; import java.util.LinkedList; import android.app.Application; import android.content.res.Configuration; public class GlobalData extends Application{ private String email; private String itemName; private int itemQuantity; private int itemCost; public GlobalData(){ } public GlobalData(String iName,int iQunt,int iCost) { // TODO Auto-generated constructor stub this.itemCost=iCost; this.itemName=iName; this.itemQuantity=iQunt; } public void setEmail(String mail) { this.email=mail; } public String getEmail() { return email; } public String getItemName() { return itemName; } public int getItemCost() { return itemCost; } public int getItemQunt() { return itemQuantity; } LinkedList<GlobalData> list = new LinkedList<GlobalData>(); public void setList(String iName,int iQunt,int iCost) { list.add(new GlobalData( iName, iQunt, iCost)); } public LinkedList<GlobalData> getList() { return list; } public void removeItem(String iName,int iQunt,int iCost) { for(GlobalData data:list) { if(data.getItemName().equals(iName)) { list.remove(itemName); //list.remove(iCost); //list.remove(iQunt); } } } } 
0
source share
5 answers

Override the equals() method first and use the itemName attribute to compare

 @Override public boolean equals(Object o) { if (o == null) return false; if (itemName == null) return false; if (o instanceOf String) return itemName.equals(o); else if (o instanceOf GlobalData) return ((GlobalData) o).itemName.equals(this.itemName); else return false; } 

Then change the removeItem() method

 public void removeItem(String iName) { list.remove(iName); // or uncomment line below to completely remove all matching elements // for (;;list.remove(iName)) {} } 

According to http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html#remove (java. Lang.Object) remove() the LinkedList method will call the supplied Object equals() method and compare him with all the items in the list.

Hope this helps :)

+1
source

you cannot work in lists (add, delete ... items) while you repeat them. You must use iterator

 for(Iterator<EmpDedup> iter = list.iterator(); iter.hasNext();) { EmpDedup data = iter.next(); if (data.getRecord() == rec1) { iter.remove(); } } 

see http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html

Link to fooobar.com/questions/1498937 / ...

0
source

You cannot modify a collection using the for-each loop. Use a simple for or while .

An iterator is hidden for each loop, so you cannot call remove. Therefore, for each cycle it is not used for filtering. It is likewise not used for loops where you need to replace items in a list or an array when you go through it.

Source: http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

Typically, a collection called list that stores elements of type E uses an iterator as follows:

 Iterator<E> iterator = list.iterator(); while(iterator.hasNext()) { <do something with iterator.next()>; } 
0
source

Use an iterator to remove an element:

 public void removeItem(String iName, int iQunt, int iCost) { Iterator<GlobalData> iterator = list.iterator(); while (iterator.hasNext()) { GlobalData data = iterator.next(); if (data.getItemName().equals(iName)) { iterator.remove(); } } } 
0
source

- . You simultaneously access and modify the Collection , which cannot be executed directly from the For-Each loop.

- Use Iterator to solve this problem.

 LinkedList<GlobalData> q1 = new LinkedList<GlobalData>(); Iterator<GlobalData> iterator = q1.iterator(); while (iterator.hasNext()){ GlobalData mp = iterator.next(); if (mp.name.equals("xyz")){ iterator.remove(); // You can do the modification here. } } 
0
source

Source: https://habr.com/ru/post/1498927/


All Articles