Skip to content
Advertisement

How can I correctly implement the methods in my class?

I have a class SimpHisto that takes in an array of generic type SL, and returns the size and the count of a specific element of that array. My code is provided down below.

JavaScript

After I tried to run my code using the snippet down below…

JavaScript

I get an error saying:

JavaScript

Can someone please explain what’s wrong with my code and provide any explanations on solutions?

Thank you

P.S. this is interface Histo which is implemented by SimpHisto

JavaScript

Advertisement

Answer

The error is in your Iterate. You throw that exception if (index <= items.length). index starts with 0 so this is always true. You only have to change to >=

As mentioned by 英語は苦手 your while in getCount does not call next. Since you iterate over the items directly I would prefer to remove the Iterator at all. And a Stream may increase readability:

JavaScript

EDIT: without Stream-API you can iterate over your items directly:

JavaScript

If you want to use the Iterator you have to call next

JavaScript

BTW: why assertEquals(3, elemCount);? There are 4 elements

Advertisement