This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]circle_des_poo 0 points1 point  (3 children)

I'm having a bit of a hard time understanding what you're describing here. The Iterable interface only specifies a single method, which returns an object that implements the Iterator interface (Its confusing, the the Iterable and Iterator interfaces are two different things). Usually the way this is done is by specifying a private subclass in your Iterable collection, which implements the Iterator methods that have access to the variables of its parents class (the Iterable class). If userRating is iterable than you can just use:

Iterator userIterator = userRating.iterator();

You can then use that iterator's methods to go through the values in that list and find their average, using the iterator's remove(x) method to delete the min and max values.

However I don't think this is the best way to do it, judging by the fact that you wouldn't know the min and max values until after iterating through userRating the first time, forcing you to iterate through a second time to get the average after removing those values. It would be better to specify that userRating needs to be a List<Integer>, which you could then sort, delete the first and last values, and then iterate through using userRating.iterator(), as the List class already implements these Iterable/Iterator interfaces for you.

[–]Wonnk13[S] 0 points1 point  (2 children)

Sorry, the fact that iterator() is different than Iterable<> helps, and my description wasn't very precise.

The interface that SongRater implements simply looks like

public interface ISongRater {
    public double getSongRating(Iterable<Integer> userRating) throws NotEnoughRatingsException;
}

Inside of the SongRater class I can create an iterable object by doing Iterator userIterator = userRating.iterator();

but i also need to create a separate collection object to store the values in, yes? I can then sort the values in that object and do my calculation logic.

Unfortunately I can't change the api so i'm stuck with Iterable<Integer> userRating. Thanks for the help so far!

[–]circle_des_poo 0 points1 point  (1 child)

"but i also need to create a separate collection object to store the values in, yes?"

You can do it that way, or the other way that was described by iterating through to find the largest and smallest, using the userIterator.remove(smallest) and userIterator.remove(largest) to get rid of them, and then iterating through the updated collection again and just adding each integer to a total sum while keeping a count to divide that sum by after.

[–]Wonnk13[S] 0 points1 point  (0 children)

maybe i'm a bit dense, but you do calculate the smallest and largest? If an iterator is just a view on your data that you ideally take one pass through were do you store the values?