Eli
Eli

Reputation: 55

How to add to new ArrayList without a conversion error?

I keep getting an error saying that void/boolean cannot be converted to ArrayList . However, I am passing the index of the largest quakeData and a QuakeEntry should be returning to be added to my new ArrayList answer. I do not know how to fix this.

public ArrayList<QuakeEntry>getLargest(ArrayList<QuakeEntry> quakeData, int howMany) {
    ArrayList<QuakeEntry> answer = new ArrayList<QuakeEntry>();
    int bigIndex = indexOfLargest(quakeData);
    answer = answer.add(quakeData.get(bigIndex));

    return answer;
}

Upvotes: 0

Views: 28

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521864

I think you just need to return the answer list:

public ArrayList<QuakeEntry>getLargest(ArrayList<QuakeEntry> quakeData, int howMany) {
    ArrayList<QuakeEntry> answer = new ArrayList<QuakeEntry>();
    int bigIndex = indexOfLargest(quakeData);
    answer.add(quakeData.get(bigIndex));

    return answer;
}

The List#add() method returns a boolean value, which would be true if the addition were successful. It doesn't make sense to try to assign the result of List#add to your answer list. But, in your case, the method requires that you return a List<QuakeEntry>, so just do that.

Upvotes: 1

Related Questions