• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterator type problem  RSS feed

 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, well I've been over this school problem for two days, for other matter il try to ask laster
by now I decided to slice my big problem into little parts, sooo....







when I write that expression on my main method, i get a message saying:

incompatible types, object can not be converted to boolean,

this is getting me confused, cause the method getEquals() returns a boolean iterator

and then im simply using next method over getEquals(), I suppose that should be a boolean,

what is going on people?


open to sugestion brothers,

thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show what you put for c1, c2 and cmp? I picked the following and the code compiled for me



Just curious - why don't you just return the boolean directly? What is the purpose of returning an iterator with a single boolean object?

And good instinct on splitting the problem in smaller parts. That really is a good way to program and problem solve.
 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
damm! I've made a mistake, the method type is Iterable<Boolean> not Iterator<Boolean>

i can now return an Iterable,

the goal of the exercice is receiving two collections c1, c2... and a comparator cmp..(lexicographical)



execute on the main method this expression:


for (boolean o : getEquals(c1, c2, cmp))
System.out.println(o);


which will return booleans, according to the comparator result

still getting the same problem tho :s
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show your new getEquals method implementation?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaime Caetano wrote:hey, well I've been over this school problem for two days...

OK, well there's quite a lot of stuff going on here, so a good practise is usually to write out what needs to be done, in detail, and in English (or your native language) first - before you write a single line of code.

However, just from what I see:

1. Your loop continues while both collections have more elements, which suggests that your Iterable<Boolean> will be the same length as the shorter Collection. It's not necessarily an error, but it might be worth documenting why you chose this approach.

2. You're using a Comparator and compare() to return the result of something for a method called getEquals(). Again, not necessarily a problem, but it might be worth documenting why; otherwise it begs the question: Why don't you just use equals()?

3. (A very minor performance thing) You're adding booleans, which have to be boxed to Booleans for your output Iterable. If you wroteinstead, your code would probably run faster. In this case you'll probably never notice the difference, but it's worth bearing in mind; particularly if multiple boxing/unboxing conversions get involved.

HIH

Winston

PS: Please don't edit your posts after the fact if you're asking questions about code you've posted, because it tends to make nonsense of the replies you get. Thanks.
 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Winston,

thanks a lot for the tips, I really apreciatte your attention,

this exercice is academic, they want me to understand what is a comparator, anonimous class, collections, iterators all together,

thats the Bolonha system in Portugal, but that's other story which im not willing to complain about.

I might need to go to the root of my problem, interfaces.

so I will create another post,

thank you, Jaime
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!