创建Item对象的子集

时间:2012-09-01 07:03:33

标签: java

嘿伙计们我有用于创建Item对象的类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类来存储项目列表。这里添加了项目对象列表

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)
{
    //hold = new ArrayList<Item>();
    this.hold.add(item);
}


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



}

首先,我将I1,I2,I3项目添加到Itemlist,如下所示

 {I1}
 {I2}
 {I3}

现在我想创建一个Item对象的子集,如下所示,并添加到ItemList,其中每个子集将包含2个项目

     {I1 I2}
     {I1 I3}
     {I2 I3}

请帮助创建子集  后来我甚至想要有2个或更多项的子集  我想编写一个函数来帮助我创建n个项的子集  请帮忙

1 个答案:

答案 0 :(得分:0)

我个人认为你的ItemList类API不会帮助你实现目标。
您需要的是使用Sets,以便使用“Set theory”API (即十字路口,工会等) ItemList类可能应该有一个“asSet”方法,将ItemList转换为Set。
您需要实现“PowerSet”计算才能获得所有可能的子项集。
使用Set API实现这一点比使用它更容易。
你也可以使用互联网上的一些开源 - 例如,我现在用Google搜索,找到了this -
查看此URL指向的类实现的PowerSet接口。