Im working with the following method:
public void m(List<? extends Object[]> objs){
objs.stream()
.map(oa -> oa[0]) //compile error
//array type expected
.forEach(System.out::println);
}
DEMO
Why doesn't it work? I thought everything that extends array can be viewed as an array. Actually I can get length
from the array.
This error has the following cause and solution: The syntax you specified is appropriate for an array, but no array with this name is in scope. Check to make sure the name of the variable is spelled correctly. Unless the module contains Option Explicit, a variable is created on first use.
An array is a type of variable which differs from a 'normal' variable in that it can hold multiple values rather than just one value at a time. There can be a few reasons why you would receive an “Expected array” error.
There is actually no such class that extends Object[]
; each array has a fixed type and its own class, eg MyClass[].class
You should use a typed method:
public <T> void m(List<T[]> objs){
objs.stream()
.map(oa -> oa[0]) // no compile error
.forEach(System.out::println);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With