我怎样才能正确地移除ArrayList的对象? [英] How can I correctly remove an Object from ArrayList?
问题描述
我想从一个ArrayList中删除对象。每个项目对象有3个属性; 1. itemNum 2.信息3.成本。我也有3班,1项类定义存储在目录中的单品。 2.目录类保持了项目的对象列表。 3客户端类W / main方法。
我有集和项类获取和我在目录ArrayList中。在客户端,我有一个提示在itemNum为回车清除。我如何正确地移除ArrayList中基于一个itemNum搜索Item对象?下面是我的code和我到目前为止已经试过。
I’m trying to remove an object from an ArrayList. Each Item object has 3 attributes; 1. itemNum 2. info 3. cost. I also have 3 classes, 1. Item class defines the single items stored in the catalog. 2. Catalog class maintains the list of the Item objects. 3 Client class w/main method. I have the sets and gets in Item class and I have the ArrayList in Catalog. In Client, I have a prompt to "Enter in the itemNum to remove. How do I correctly remove an Item object from the ArrayList based on a search for an itemNum? Below is my code and what I’ve tried so far.
Public class Item
{
private int itemNum;
private String info;
private double cost;
private int itemNum;
public Item()
{ //start constructor
itemNum = 0; //default values
info = "x";
cost = 0;
} //end constructor
public CatalogItem(int newItemNum, String newInfo, double newCost)
{ //start overload constructor
this.itemNum = newItemNum;
this.info = newInfo;
this.cost = newCost;
} //end overload constructor
//下面是itemNum设置/得到。我也有设置/获取成本和信息,但选择不包括做空间
//below are the set/gets for itemNum. I also have sets/gets for cost and info, but choose not to include do to space
public int getItemNum()
{ //start itemNum accessor
return itemNum;
} //end getItemNum
public void setItemNum(int newItemNum)
{ //start itemNum mutator
this.itemNum = newItemNum;
} //end setItemNum
} //end Item class
public boolean equals(CatalogItem obj)
{ //start equals
if (itemId == obj.itemId)
return true;
else
return false;
} //end equals
//下面是我的目录类
//below is my Catalog Class
import java.util.*;
public class Catalog
{ //start class
private ArrayList<CatalogItem> listOfObjects = new ArrayList<CatalogItem>(100); //creates ArrayList
Item newItem = new Item(newItemNum, newInfo, newCost);
public void remove(int id)
{ //start remove
int item = id;
for (int index = 0; index < listOfObjects.size(); index++)
if (newItem.getItemId() == item) //if item in the inventory matches the item number passed
listOfObjects.remove(index); //removes based on index, I’ve also tried listOfObjects.remove(item);
/* I’ve also tried an enhanced for loop
for (CatalogItem obj : listOfObjects)
if (newItem.getItemId() == item)
listOfObjects.remove(newItem); */
} //end remove
}
//下面是主要的。它接收来自有关itemNum,信息,和成本,用户输入
//below is main. It receives input from the user regarding the itemNum, info, and cost
import java.util.*; //allows use of Scanner class
public class Client
{ //start client class
public static void main(String[] args)
{ //start main
Catalog serv = new Catalog(); //creates instance of Catalog class
Scanner scan = new Scanner(System.in); //creates instance of Scanner class called scan
System.out.print("\nEnter in the Item ID you want to remove: ");
id = scan.nextInt();
serv.remove(id); //sends id to Catalog Class to be removed
} //end main
} //end class
它编译罚款,但不会删除基础上创建索引。任何帮助将是巨大的。
It compiles fine, but doesn't remove based on the found index. Any help would be great.
推荐答案
覆盖等于
方法在项目
类。您可以使用 itemNum
来检查对象平等的equals方法。
Override equals
method in your Item
class. You can use itemNum
to check the equality of objects in your equals method.
然后使用ArrayList 删除(对象o)
方法来删除的对象。在删除
方法使用等于
内部找到要删除的对象。
Then use ArrayList remove(Object o)
method to delete the object. The remove
method uses equals
internally to find the object to be removed.
编辑:
您没有覆盖equals方法得当,这里是正确的签署和实施:
You are not overriding the equals method properly, here is the right signature and implementation:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item other = (Item) obj;
if (itemNum != other.itemNum)
return false;
return true;
}
这篇关于我怎样才能正确地移除ArrayList的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!