[英]Java how to remove an object from one ArrayList while adding it to another?
This is my first time asking on this site, and got my exam tomorrow so trying to make my code great! 这是我第一次在此网站上提问,明天又通过了考试,因此请尝试使我的代码更好! I will link the whole class (Even though its 3 others). 我将链接整个班级(即使是其他3个班级)。
This is the method, and I cant manage to remove an object from lagerplass
, adding it to hk.addVarer
works tho. 这是方法,我无法设法从lagerplass
删除对象,将其添加到hk.addVarer
可以。 The idea behind all this is that lagerplass
is were the items is stored in a store(butikk), so when people buy an item from the store and put it in handlekurv
the items gets removed from the storage(lagerplass) : 所有这些背后的想法是, lagerplass
是将物品存储在商店中(butikk),因此,当人们从商店购买物品并将其放入handlekurv
,物品会从仓库中移除(lagerplass):
public void leggTilHandlekurv(String varenavn)
{
for (Varer a : lagerplass)
{
if (a.getVarenavn().equals(varenavn))
{
hk.addVarer(a);
lagerplass.remove(varenavn);
}
}
}
Here is the kode for the whole Butikk class 这是整个Butikk课的主题
public class Butikk
{
// instance variables - replace the example below with your own
public ArrayList<Varer> lagerplass;
Handlekurv hk = new Handlekurv();
/**
* Constructor for objects of class Butikk
*/
public Butikk()
{
// initialise instance variables
lagerplass = new ArrayList<Varer>();
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void nyHeadset(String lyd, String vare,int pris, String varenavn )
{
// put your code here
Headset nyHeadset = new Headset ( lyd, vare, pris, varenavn);
lagerplass.add(nyHeadset);
}
public void nyMus(String vare, int pris, String varenavn, int dpi)
{
Mus nyMus = new Mus(vare, pris, varenavn, dpi);
lagerplass.add(nyMus);
}
public void printLagerplass()
{
for (Varer vare : lagerplass)
{
vare.printDetails();
}
}
public int lagerplassSize()
{
return lagerplass.size();
}
public void fjernHeleLagerplass()
{
lagerplass.clear();
}
public void leggTilHandlekurv(String varenavn)
{
Iterator<Varer> iterator = lagerplass.iterator();
while(iterator.hasNext()) {
Varer a = iterator.next();
if (a.getVarenavn().equals(varenavn)) {
iterator.remove();
hk.addVarer(a);
}
}
} }
public void printHeleHandlekurven()
{
hk.printHandlekurv();
}
}enter code here }在这里输入代码
Here is the kode for the whole Varer class 这是整个Varer课程的主题
enter code here public class Varer { // Representerer merke og pris til en vare. 在此处输入代码public class Varer {//代表代理人。 private String vare; 私有字符串变量; private String merke; 私有String merke; private int pris; 私人诠释 private String varenavn; 私有字符串varenavn;
/**
* Constructor for klassen Varer
*/
public Varer(int pris, String varenavn, String vare)
{
merke = "Razor";
this.pris = pris;
this.varenavn = varenavn;
this.vare = vare;
}
public String getMerke()
{
return merke;
}
public int getPris()
{
return pris;
}
public String getVarenavn()
{
return varenavn;
}
public String getVare()
{
return vare;
}
public String getDetails()
{
return vare + ", " + merke +" " + varenavn + ", "+pris + " kr.";
}
public void printDetails()
{
System.out.println(vare + ", " + merke +" "+ varenavn + ", "+pris + " kr.");
System.out.println();
}
Removing an item from a collection while iterating over it should yield a ConcurrentModificationException
, which I presume is what you are getting and is why you cannot remove from it. 从一个集合中删除一个项目,同时对其进行迭代,应该会产生ConcurrentModificationException
,我想这就是您所得到的,这就是为什么您不能从其中删除它。
To remove an element while iterating over a collection, you will need to get that collection's iterator and remove it through the collection's iterator. 要在迭代集合时删除元素,您将需要获取该集合的迭代器,并通过集合的迭代器将其删除。
public void leggTilHandlekurv(String varenavn) {
Iterator<Varer> iterator = lagerplass.iterator();
while(iterator.hasNext()) {
Varer a = iterator.next();
if (a.getVarenavn().equals(varenavn)) {
iterator.remove();
hk.addVarer(a);
}
}
}
Iterator.remove
is the only safe way to modify a collection during iteration; Iterator.remove
是在迭代过程中修改集合的唯一安全方法。 the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress. 如果在进行迭代时以任何其他方式修改了基础集合,则行为未指定。
for (Iterator<Varer> iterator = lagerplass.iterator(); iterator.hasNext();) {
Varer a = iterator.next();
if (a.getVarenavn().equals(varenavn))
{
hk.addVarer(a);
// Remove the current element from the iterator and the list.
iterator.remove();
}
}
Source: 资源:
http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
It is because you cannot modify a collection while iterating over it. 这是因为您无法在迭代集合时修改集合。 Either use a for loop with indeces, or an Iterator over your collection and call the remove method. 使用带indees的for循环,或在集合上使用Iterator并调用remove方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.