2012-08-31 49 views
0
迭代項目

嘿傢伙我無法迭代或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) { 

} 

但我得到的錯誤。

+1

你得到什麼錯誤? – Behe

+1

單詞「set」的使用會混淆關鍵字迭代的含義......我會使用不同的單詞,但它與您的問題無關。 –

+1

嗯,你創建了很多項目列表。就像每次添加項目一樣。可能是一個問題。持有ItemSet列表而不是隻有一個列表的目的是什麼? –

回答

2

爲了使用 '的foreach' 語句,你ItemSet類應該實現java.lang.Iterable接口

UPDATE:

看到這個SO answer有關如何實現Iterable接口

+0

確定,怎麼了supossed使用,可以請你告訴我 這樣做 @覆蓋 公共迭代器迭代器(){ 拋出新UnsupportedOperationException異常(「尚不支持。」); } – Nyfer

+0

請看我上面的更新,那麼SO答案應該適合你。 –

2

爲了能夠使用for (Item i : itemSet),您的ItemSet類必須實現iterable接口。你可以通過添加下面的方法將ItemSet類做到這一點:

public Iterator<Item> iterator() { 
    return hold.iterator(); 
} 

請記住,你應該添加implements Iterable<Item>到類聲明。您可以隨時使用for (Item i : itemSet.getItemSet())

+0

其實我想從項目集項目,因爲我正在添加項目 itemSet.getItemSet()將返回項目集 – Nyfer

+0

itemSet.getItemSet()返回包含所有項目的列表對嗎?這個列表本身實現了可迭代接口,因此你可以用foreach循環它。這樣,ItemSet將不必實現Iterable接口。 – arshajii

+0

只得到最後一個項目I4不是全部,即I1,I2,I3 – Nyfer

0

getUniqueItem()方法在Transaction類返回一個ItemSet不落實Iterable接口,所以你不應該期望能夠遍歷它。您需要通過調用getItemSet()來獲得ItemSet中的List<Item>並重復此操作。

順便說一下,getItemSet()可能不是該方法的一個很好的名稱,因爲它返回List<Item>

+0

是無法從Transaction類迭代項集 我實現迭代如下 @Override 公共迭代迭代(){ 迭代 itemList中= trans.iterator(); return itemList; } – Nyfer

0

我認爲你的代碼沒有做出很多意義,我嚴重不明白爲什麼每個人都想實現Iterator接口。

首先您使用名稱Set來設置包含List的類。 然後,您在addItems()方法中重新創建List的新實例,該方法應該是addItem(),因爲您要將單個項目添加到列表中。

你在做什麼基本上是做一個列表並添加一個項目給它,但是你添加的下一個項目重新創建列表,並且將它的引用分配給你之前列表的引用,從而有效地刪除它。 因此,你的列表永遠不會包含多於一個項目。

如果你想有一個列表作爲一個類的屬性,那麼類的名稱,你可以直觀,就像ITEMLIST例如,因爲它告訴你,你有一個項目清單,這正是它是。 但是,如果它只包含一個列表,那麼實際上它不需要,因爲您唯一要做的就是在兩者之間添加一個方法調用。你可以使用List接口或任何實現它的類,比如ArrayList。

我把你的代碼和重寫它,因爲它會有道理。 我希望它可以幫助你;)

public class Transaction { 

    public ItemList getUniqueItems() { 

     Database d = new Database(); 
     ItemList unique = new ItemList(); 
     String query="Select id,name from item"; 
     ResultSet rs = d.sendQuery(query); 
     try { 
      while(rs.next()) { 
       System.out.println(rs.getString(1)+"\t"+rs.getString(2)); 
       unique.addItem(new Item(rs.getString(1),rs.getString(2)));  
      } 
     } 
     catch(Exception e){ 
      System.out.print(e.getMessage()); 
     } 
     return unique; 
    } 

} 

public class Ap { 

    public static void main(String args[]) { 
     Transaction t = new Transaction(); 

     ItemList i = t.getUniqueItems(); 
     List<Item> list = i.getItemList(); 
     list.get(10); //11 because 0 counts also 
    } 
} 

較短的版本是: 1:TRANSATION類

public class Transaction { 

    public List<Item> getUniqueItems() { 

     Database d = new Database(); 
     List<Item> unique = new ArrayList<Item>(); 

     String query="Select id,name from item"; 
     ResultSet rs = d.sendQuery(query); 
     try { 
      while(rs.next()) { 
       System.out.println(rs.getString(1)+"\t"+rs.getString(2)); 
       unique.add(new Item(rs.getString(1),rs.getString(2)));  
      } 
     } 
     catch(Exception e){ 
      System.out.print(e.getMessage()); 
     } 
     return unique; 
    } 

} 

2:鴨類:

public class Ap { 

    public static void main(String args[]) { 
     Transaction t = new Transaction(); 
     List<Item> list = t.getUniqueItems(); 
     list.get(10); //11 because 0 counts also 

       // iteration with a foreach loop 
       for (Item i : list) { 
        System.out.println(i); 
       } 

       // iteration with a for loop 
       for (int n = 0, s = list.size(); n < s; n++) { 
        System.out.println(list.get(n)); 
       } 

       // iteration with a Iterator 
       Iterator<Item> iterator = list.iterator(); 
       while (i.hasNext()) { 
        System.out.println(i.next()); 
       } 
    } 
} 

所以有完全不需要在任何地方實現Iterator接口。

+0

比原來好得多......我仍然覺得方法名稱「getUniqueItems」是誤導性的。 –

+0

你能告訴我如何從交易 – Nyfer

+0

獲得ItemList對不起,我編輯它。變數我不在那裏了。所以它變成了t,它是包含getUniqueItems()方法的事務。 – fonZ