0

hey guys am not able iterate or get items from ItemSet following is my code

Item class

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

}

ItemSet Class holds List of items

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

}

This is my Transaction class holds list of ItemSets

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

}

And this is my main class

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

I dont know how to get Items from ItemSet at 11

I tried using

foreach(Item i:I) {

}

But I am getting error.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Nyfer
  • 163
  • 2
  • 5
  • 15
  • 1
    And what error are you getting? – Behe Aug 31 '12 at 19:13
  • 1
    Use of the word "set" has confusing implications about iteration over keys... I'd use a different word, but it has nothing to do with your problem. – Steve H. Aug 31 '12 at 19:14
  • 1
    Hmm, you create an awful lot of item lists. Like every time you add an item. Could be an issue. What's the purpose of holding a list of `ItemSet`s instead of just having a single list? – Dave Newton Aug 31 '12 at 19:14
  • 1
    The wrongly-named "addItems", (which takes a single Item) will always wipe out the existing list and replace it with a new one containing a single item. – Steve H. Aug 31 '12 at 19:19
  • @SteveH. Yep, probably copy/pasted from the initializing constructor. – Bill the Lizard Aug 31 '12 at 19:28

4 Answers4

2

In order to use 'foreach' statement, your ItemSet class should implement java.lang.Iterable interface

UPDATE:

See this SO answer about how to implement Iterable interface

Community
  • 1
  • 1
su-
  • 3,116
  • 3
  • 32
  • 42
  • ok and how am supossed to use that can you please tell me have done this @Override public Iterator iterator() { throw new UnsupportedOperationException("Not supported yet."); } – Nyfer Aug 31 '12 at 19:21
  • please see my update above, that SO answer should work for you. – su- Aug 31 '12 at 19:23
2

To be able to use for (Item i : itemSet), your ItemSet class must implement the iterable interface. You could do this by adding the following method to the ItemSet class:

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

Remember that you should add implements Iterable<Item> to the class declaration.

Note that you could always use for (Item i : itemSet.getItemSet()).

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • actually i want the item from itemSet since am adding items to it itemSet.getItemSet() will return ItemSet – Nyfer Aug 31 '12 at 19:24
  • itemSet.getItemSet() returns the list containing all of the items right? This list itself implements the iterable interface therefore you can loop through it with foreach. This way, ItemSet won't have to implement the Iterable interface. – arshajii Aug 31 '12 at 19:32
  • am getting only the last item I4 not all ie I1,I2,I3 – Nyfer Aug 31 '12 at 19:36
  • why would you even do that? This is just overhead... the list you have already has an iterator. Check what i answered below. – fonZ Aug 31 '12 at 19:41
  • In transaction class am retrieving items from db adding them to itemset and the returning to main. i have 4 items in db which means i shud get all 4, but am getting only I4 wen i iterate using the following code for(Item i:I) { System.out.println(i.getItemId()+ ":"+ i.getItemName()); } – Nyfer Aug 31 '12 at 19:46
0

The getUniqueItem() method in your Transaction class returns an ItemSet which does not implement the Iterable interface, so you shouldn't expect to be able to iterate over it. You need to get the List<Item> from the ItemSet by calling getItemSet() and iterate over that.

By the way, getItemSet() is probably not a great name for that method, since it returns a List<Item>.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • am not able to iterate itemsets from Transaction class i implemented the iterator as follows @Override public Iterator iterator() { Iterator itemList = trans.iterator(); return itemList; } – Nyfer Aug 31 '12 at 20:08
0

I think your code doesnt make alot of sense and i seriously dont understand why everybody wants to implement the Iterator interface.

First you make use the name Set for a class that contains a List. Then you recreate a new instance of the List in the addItems() method, which should be addItem() because you want to add individual items to your list.

What you are doing is basically making a list and adding an item to it but the next item you add you recreate the list and you assign the reference of it to the reference of your previous list, thus effectively deleting it. So you list will never contain more then 1 item.

If you want to have a list as an attribute of a class, then the name of the class you be intuitive, like ItemList for example because that tells you that you have a list of items and that is exactly what it is. But if it contains only one list then actually its not needed at all because the only thing you do is add a method call in between. You can just use the List interface or any classes that implement it, like ArrayList for example.

I took your code and rewrote it like it would make sense. I hope it helps you out ;)

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

The shorter version would be: 1: the Transation class

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: The Ap class:

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

So there is totally no need to implement the Iterator interface anywhere.

fonZ
  • 2,428
  • 4
  • 21
  • 40
  • much better than the original... Still I find the method name "getUniqueItems" to be misleading. – Steve H. Aug 31 '12 at 20:09
  • Can you please tell me how to get ItemList from transaction – Nyfer Aug 31 '12 at 20:26
  • Sorry, i edited it. The variable i was not there anymore. So instead it became t, which is the transaction that contains the getUniqueItems() method. – fonZ Aug 31 '12 at 20:29
  • @jonathan.cruz ya thanks I have done the following List list = t.getUniqueItems(); for(Item i:list) { ItemList itemList=new ItemList(i); Ci.addTransaction(itemList); } Now i want to iterate over Ci and send itemsets to count function in Transaction Please help me, am not able to iterate – Nyfer Aug 31 '12 at 20:32
  • Edited my answer and added a couple of ways to do what you want to do. – fonZ Aug 31 '12 at 20:41