0

Please, before pitching this into the duplicate bucket, have mercy and help a Java 'newbie'...my code is:

//at top of class
List<String> faceStrList             = new ArrayList<String>(cardsPerSuit);

Then, the line (in another method) that uses the offending other method:

faceStrList = readInfo( cardValueFileStr, faceStrList );

and the problem-child:

public static List readInfo( String inputFile, List<String> dataArrayOfStr )  
{
    String word = 'dummy';
    dataArrayOfStr.add(word);
    return dataArrayOfStr;
}

Again, please look kindly.

Benjamin Levy
  • 333
  • 6
  • 19

2 Answers2

0

You just need to tell compiler that you are returning a list of String

public static List<String> readInfo(String inputFile, List<String> dataArrayOfStr)
{
    String word = "dummy";
    dataArrayOfStr.add(word);
    return dataArrayOfStr;
}

The java.util.List interface is a subtype of the java.util.Collection interface. It represents an ordered list of objects, meaning you can access the elements of a List in a specific order, and by an index too

You have already defined faceStrList is a list of String. Then you are assigning a list of any type to the variable.

It won't create any compiler error, but it would be better if you can specify that you are returning a list of string to the compiler.

Stenal P Jolly
  • 737
  • 9
  • 20
0

Here:

public static List readInfo(

You got your generics "correct" all over the place; but not when declaring that method. You are omitting the generic type for the returned list; thus returning a raw type instead (see here for details). And that is what the compiler warns you about.

So the solution is simply to do the same thing that you did in other places, and change the method signature to

public static List<String> readInfo( 

And for the record:

List<String> faceStrList = new ArrayList<String>(cardsPerSuit);

could be simplified like

List<String> faces = new ArrayList<>(cardsPerSuit);
  • You can omit the generic type using the <> diamond operator; no need to repeat that information
  • Beyond that: there is no point in putting the collection type into your variable name. Just make clear "it is more than one". The point is: maybe you want to change the collection type later on. Believe me, you will get tired of renaming all your variables then!
  • It is not exactly good practice to return the list that comes in as parameter already. And it is also not required - adding something to the list like you do already affects that incoming list. To return that list simply makes your interface more "complicated" to understand.
Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248