0

这是我的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(ItemList itemset) {
    Item item=null;
    return item;
  }
}

这是我的ItemList课程,其中将包含以下List内容items

public  class ItemList implements Iterable<Item>  {

  private List<Item> hold=new ArrayList<Item>();



  ItemList(Item item) {
    this.hold.add(item);
  }

  ItemList() {
    //throw new UnsupportedOperationException("Not yet implemented");
  }


  public List<Item> getItemSet() {
    return this.hold;
  }


  public void addItems(Item item) {
    this.hold.add(item);
  }


  @Override
  public Iterator<Item> iterator() {
    Iterator<Item> item = hold.iterator();
    return item;
  }
}

这是Transaction课程:

public class Transaction implements Iterable {

 private List<ItemList> trans=new ArrayList<ItemList>();

 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;
}

public ItemList getUniqueItem() {
     ResultSet rs;
     Database d=new Database();
     ItemList unique=new ItemList();
      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 void addTransaction(ItemList itemList) {
     this.trans.add(itemList);
}

public List<ItemList> getTransaction() {
     return this.trans;
}

@Override
public Iterator<ItemList> iterator() {
    Iterator<ItemList> itemList = trans.iterator();
    return itemList;
}

public int countItems(ItemList itemList) {
      ResultSet rs;
      Database d=new Database();
      String query="";
      int count=0;
      for(Item i:itemList)
     {
       System.out.println(i);
       String id=i.getItemId();
        System.out.print(id+"\t");
        query="SELECT count(*) FROM `item_transaction` where item_id="+id;
          rs=d.sendQuery(query);
          try{
           while(rs.next())
          {
              System.out.print(rs.getString(1));
              count=Integer.parseInt(rs.getString(1));
              System.out.print(count+"\t");
          }

          }catch(Exception e){}
   }

      return count;
 }

 }

这是我的main课:

public class Ap {

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

    List<Item> list = t.getUniqueItems();
    for(Item i:list)
    {
        ItemList itemList=new ItemList(i);
        Ci.addTransaction(itemList);
    }

}

添加itemList到之后Transaction,我想迭代Ci并传递itemList给类中的countItems方法Transaction

我试过:

for(ItemList list:Ci) {
    int x=t.countItems(list);
}

但我得到:

found  java.lang.Object
      Required ItemList

还是我必须List<ItemList>Transaction课堂上获取然后迭代?

4

1 回答 1

0

你需要做

 class Transaction implements Iterable<ItemList>

正如您所说,它是普通对象的可迭代对象。

于 2012-08-31T20:54:32.427 回答