嘿傢伙我無法迭代或ItemSet
下得到物品是我的代碼不能從套裝
Item類
public class Item {
private String id;
private int count;
private String name;
public int getcount() {
return this.count;
}
public Item(String name) {
this.name=name;
this.id = "";
}
public Item(String id, String name) {
this.name=name;
this.id=id;
}
public Item(int count) {
this.count=count;
}
public String getItemName() {
return this.name;
}
public String getItemId() {
return this.id;
}
public Item returnItems(ItemSet itemset) {
Item item=null;
return item;
}
}
套裝類持有項目清單
public class ItemSet {
private List<Item> hold;
ItemSet(Item item) {
hold = new ArrayList<Item>();
this.hold.add(item);
}
ItemSet() {
//throw new UnsupportedOperationException("Not yet implemented");
}
public List<Item> getItemSet() {
return this.hold;
}
public void addItems(Item item) {
hold = new ArrayList<Item>();
this.hold.add(item);
}
}
這是我的交易類持有的物品集列表
public class Transaction {
private List<ItemSet> trans;
public ItemSet getUniqueItem() {
ResultSet rs;
Database d=new Database();
ItemSet unique=new ItemSet();
String query="Select id,name from item";
rs=d.sendQuery(query);
try{
while(rs.next()) {
System.out.println(rs.getString(1)+"\t"+rs.getString(2));
Item item=new Item(rs.getString(1),rs.getString(2));
unique.addItems(item);
}
} catch(Exception e) {
System.out.print(e.getMessage());
}
return unique;
}
}
這是我的主類
public class Ap {
public static void main(String args[]) {
Transaction t=new Transaction();
Transaction Ci=new Transaction();
Transaction Li=new Transaction();
ItemSet I=t.getUniqueItem(); //11
}
}
我不知道如何從套裝在11
得到物品我嘗試使用
foreach(Item i:I) {
}
但我得到的錯誤。
你得到什麼錯誤? – Behe
單詞「set」的使用會混淆關鍵字迭代的含義......我會使用不同的單詞,但它與您的問題無關。 –
嗯,你創建了很多項目列表。就像每次添加項目一樣。可能是一個問題。持有ItemSet列表而不是隻有一個列表的目的是什麼? –