jimmyC
jimmyC

Reputation: 563

Having trouble loop through a list <set <pair > >

Here is how I declared my list. it.next() appears to be returning my set when it should be returning a pair (which is a string,int pair) in one of my lists. Any ideas? The method getFirst() is undefined for the type Object..It seems that if I do this , it fixes this.

String m=((Pair) it.next()).getFirst();

List <HashSet<Pair>>  addresses = new ArrayList <HashSet<Pair>> ();

for (int i = 0; i < 100; i++) {
    Iterator it = (addresses.get(i)).iterator();
    while (it.hasNext()){
        String m = it.next().getFirst()); //getFirst returns a string
    }
}

Upvotes: 2

Views: 915

Answers (2)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

Try this, it compiles just fine:

List<Set<Pair>> addresses = new ArrayList<Set<Pair>>();
// fill the list of addresses

for (int i = 0; i < 100; i++) {
    Iterator<Pair> it = addresses.get(i).iterator();
    while (it.hasNext()) {
        String m = it.next().getFirst();
    }
}

Of course, you'll have to populate the list of addresses with sets of pairs for the above to do something useful.

Upvotes: 6

shazeline
shazeline

Reputation: 503

Make sure to remove the extra parenthesis at the end of the line inside the while loop. It should look like this

String m = it.next().getFirst();

Upvotes: 1

Related Questions