Hello :) I'm having a problem coping the elements of one ArrayList in my actual program to a class. In the main method of my program is the following (note that this entry is simplified; all these variables have already been declared and there is a value stored in the numPlayers variable):
ArrayList<Integer> answers = new ArrayList<>();
for(int counter = 0; counter < numPlayers; counter++)
{
System.out.print("Enter the number you think answers the question best: ");
playerAnswer = keyboard.nextInt();
answers.add(playerAnswer);
}
FindWinner survey = new FindWinner(answers);
So, as you can see, I'm using the Integer "wrapper class" to store integer entries into an ArrayList. Then, I'm passing this Integer ArrayList as an argument to the "FindWinner" constructor. This is what the constructor code looks like in the FindWinner class:
private ArrayList<Integer> usrScores = new ArrayList<>();
public FindWinner(ArrayList abc)
{
for(int counter = 0; counter < abc.size(); counter++)
usrScores.add(abc.get(counter));
}
I'm trying to copy the contents of the "answers" ArrayList into the "usrScores" field in the FindWinner class. The following error is returned:
error: no suitable method found for add(Object)
usrScores.add(abc.get(counter));
method ArrayList.add(Integer) is not applicable
(argument mismatch; Object cannot be converted to Integer)
I found something on a different Stack question on how this error is often the result of a "conversion error" between int and Integer- one is a primitive data type and the other an object. But I'm not sure why this is relevant here because I'm working with two ArrayLists that both hold Integers. Any help/contribution is much appreciated!