Java 将项目添加到苹果
我正在尝试创建一个方法来添加一个苹果。列表中的每个apple对象在存储它们的apple中都包含一个单独的列表。我只是不知道该怎么把这整件事搞定。 我是这样安排的Java 将项目添加到苹果,java,Java,我正在尝试创建一个方法来添加一个苹果。列表中的每个apple对象在存储它们的apple中都包含一个单独的列表。我只是不知道该怎么把这整件事搞定。 我是这样安排的 public class CustomerDatabase { private ArrayList<Customer> customerList = null; public CustomerDatabase() { customerList = new ArrayList<&g
public class CustomerDatabase {
private ArrayList<Customer> customerList = null;
public CustomerDatabase() {
customerList = new ArrayList<>();
}
}
公共类客户数据库{
private ArrayList customerList=null;
公共客户数据库(){
customerList=新的ArrayList();
}
}
尝试以下方法:
public void addProduct(String n, String p)
{
// loop though the Customers
for (Customer c : customerList)
{
// look for the one. i do not know what n is, assumed name from paramter n
if (n.equals(c.getName()))
{
// when found:
// add product to the wishlist, checking it is not on the list already
if (!c.getWishlist().contains(p))
c.getWishlist().add(p);
// if adding order of the wishlist is not important, another collection type
// EG a Set, which can only hold a value once, might be useful...
// exit the method, without throwing exception
return;
}
}
// if the loop finished, no Customer was found, throw exception
throw new IllegalArgumentException();
}
你在和我合作吗