0

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!

TobyTuck
  • 25
  • 5
  • I supppose that keyboard is your Java.util.Scanner? The method nextInt() returns an ´int´ value. That is what probably gives the error. You can cast it to an Integer object `new Integer(playerAnswer)` – DrummerMann Jan 07 '22 at 21:21
  • 3
    `abc` parameter type is not fully specified and so it defaults to `ArrayList`. Change `FindWinner(ArrayList abc)` to `FindWinner(List abc)` – MartinBG Jan 07 '22 at 21:24
  • You can also do something like. `usrScores.addAll( abc);` or since this is a constructor. `usrScores = new ArrayList( abc);` – matt Jan 07 '22 at 21:38
  • @MartinBG this does not seem to fix the problem. Instead, I'm getting the "error: cannot find symbol" for the keyword "List." Is there anything I need to import to use this class above the ArrayList import statement? – TobyTuck Jan 07 '22 at 22:00
  • @ Tim Moore, I did read the linked article, but I am still a little confused about why my program would be using a raw type. From what I understand, a raw type would be considered an "reference type that is formed by taking the name of a generic type declaration without an accompanying type argument list." But I did declare both the "answers" and "usrScores" as containing Integer objects- so I'm not quite sure where my program is using a raw type. – TobyTuck Jan 07 '22 at 22:05
  • Thanks @matt, I did not know that there was an available method for the ArrayList class that allowed me to add the entire list at once, instead of using a loop. That's pretty handy. – TobyTuck Jan 07 '22 at 22:08
  • 2
    The problem is here: `public FindWinner(ArrayList abc)`. `ArrayList` is a raw type. Use `ArrayList` (or, preferably `List`). – Tim Moore Jan 07 '22 at 22:13
  • 1
    In java you have inheritance. So an `ArrayList` is an implementation of a `List`. Your method really only needs a `List` because all it is doing is getting the objects out of it to add them to your ArrayList. To use a list you have `import java.util.List`. This is not related to your problem. – matt Jan 07 '22 at 22:39

1 Answers1

0

Your program is using a "raw type" simply because you omit the generic parameters.

public FindWinner(ArrayList abc)

That means your array list is just full of objects, and you would need to cast.

Integer value = (Integer)abc.get(0);

Then you can add the Integer to your usrScores

usrScores.add(value);

Java "fixed" this issue by adding generics.

public FindWinner(ArrayList<Integer> abc)

Then you can get items without casting.

Integer value = abc.get(0);

Another thing that came up in the comments is the idea of List vs ArrayList. An ArrayList implements the interface List. That means you can provide an ArrayList to any method or constructor that takes a List. The advantage of using

public FindWinner(List<Integer> abc)

Is that you can provide any List implementation.

The main point. Always use your parameters with generic classes ie. It is List<Integer> or ArrayList<Integer> and not List or ArrayList.

matt
  • 10,892
  • 3
  • 22
  • 34