0

I'm struggling with getting readings to be the correct type for this code to work. I am having trouble figuring out how to get past this error. There is a bug associated with it, but I am not sure if this is relevant to me or not.

List<String> readings = new ArrayList<String>();
Matcher m = Pattern.compile("\\d{3}").matcher(s);
while (m.find()) {
    readings.add(m.group());
}
readings = readings.toArray(new String[0]);
for (int i = 0; i < numberOfElectrodes; i++) {
    packet[i] = Integer.parseInt(readings[i]);

}

It shows error:

Error:(313, 44) java: incompatible types: no instance(s) of type variable(s) T exist so that T[] conforms to java.util.List

Error:(322, 58) java: array required, but java.util.List found

Community
  • 1
  • 1
Matt Groth
  • 470
  • 4
  • 20

3 Answers3

3

The problem lies here:

readings = readings.toArray(new String[0]);

The variable readings was earlier declared as:

List<String> readings = ...;

It's type is List<String>.

The type returned by toArray is String[]. And String[] does not implement List<String>, so you can not assign the result of toArray to readings.

What you can do is; declare a new variable with the type String[]:

String[] readingsArray = readings.toArray(new String[0]);

And then refer to that variable in the below for loop:

for (int i = 0; i < numberOfElectrodes; i++) {
    packet[i] = Integer.parseInt(readingsArray[i]); // <--

Alternatively, you can skip the creation of a String[], and use an enhanced for-loop:

// readings = readings.toArray(new String[0]);
for (String element : readings) { // For each string in 'readings'
    packet[i] = Integer.parseInt(element);
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
1
  1. readings = readings.toArray(new String[0]);

    Since readings is a list, you can not assign an array to it.

  2. Again,

    packet[i] = Integer.parseInt(readings[i]);
    

    readings is not an array and to get element from a list you have to use get(). Thus it should be:

    packet[i] = Integer.parseInt(readings.get(i));
    
Shahid
  • 2,288
  • 1
  • 14
  • 24
0

You may be familiar with a language such as Python or Ruby, where variables can hold objects of any type, and even of different types at different points in the program execution. Java, however, is among the languages in which variables' types are determined when they are declared, and they never change.

In your program, the type of readings is List<String>:

               List<String> readings = new ArrayList<String>();

The return value of List.toArray() is an array. Arrays are not Lists, else there would not be much point to that method. At line 313, the compiler is complaining that you cannot assign an array to a variable of type List. The bit about type variables is a red herring.

Similar applies at line 322: since readings is a List, you cannot apply the array indexing operator to it. You could, however, use the List.get(int) method.

There are at least two possible solutions. If, for some reason not evident in the code you posted, you specifically need an array of Strings with the same elements as readings, then you can create one. For example:

String[] readingsArray = readings.toArray(new String[0]);

Then you could later do

packet[i] = Integer.parseInt(readingsArray[i]);

On the other hand, if you do not specifically need an array (which I judge more likely than not) then you can delete line 313 altogether, and at line 322 do

packet[i] = Integer.parseInt(readings.get(i));
John Bollinger
  • 160,171
  • 8
  • 81
  • 157