|
![]() |
Tomy Alexsander wrote:So i got this in my code.
But where i am returning the counter i got the error message "it can not be converted to the String"
because i am stuck on this part and i do not know where to move.
Not sure I understand that bit. Does that mean to write documentation comments or to make the return value a Set<Cat>. The latter sounds more like it. I wouldn't have made the method return HashSet but Set<Cat>? That way the compiler knows you have a Set of Cats. But I am not sure why you are exchanging a List for a Set.Tomy Alexsander wrote:. . . ensure that it is clear to any code calling this method that the HashSet contains zero or more Cat objects. . . .
No. Afraid not. I told you earlier it's best to use an Iterator on linked lists. Didn't you find a link in the tutorials showing you an example with an Iterator? I found it easily enough. You are risking going into quadratic time complexity (i.e. slow execution) on a linked list. (You won't get that problem with an array list.)Tomy Alexsander wrote:Am I in the correct way or no? . . .
There are three kinds of actuaries: those who can count, and those who can't.
A lot better, yes.Tomy Alexsander wrote:So in this way, it will be better?
Yes, as I said, it would produce the right result, but the correction to the index was awkward code. Look how much neater and simpler your code is nowbut when I debug my first version was working. . . .
Thank yousalvin francis wrote:I like how Campbell has used the for loop without an increment expression !!
I have already called it iffy design. There are many things about that assignment that I would call iffy.. . . I would never expect a getter method to modify data in the background. . . .
And that would look like this:-Note that I have removed the side‑effect, and also that I am too tired to explain that code any further. Sorry.A few hours ago, I wrote:. . . I would use a Stream<Cat>. . . .
Nice solution, but you have added another side effect. You have changed the List (field) to a Set. The requirement to take a List and return a Set is another iffy feature of this assignment.Mike Simmons wrote:. . . I, too, despise side effects on a get method. . . .
Campbell Ritchie wrote:
Nice solution, but you have added another side effect. You have changed the List (field) to a Set. The requirement to take a List and return a Set is another iffy feature of this assignment.Mike Simmons wrote:. . . I, too, despise side effects on a get method. . . .
It so often is easy, isn't it.Mike Simmons wrote:. . . But it's an easy fix: . . .
Campbell Ritchie wrote:
Only it creates a Set (generic=interface) rather than specifically a hash set (specific=implementation).
There are three kinds of actuaries: those who can count, and those who can't.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |