Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array type expected [compile-error]

Tags:

java

arrays

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.

like image 533
St.Antario Avatar asked May 11 '17 17:05

St.Antario


People also ask

What is expected array error in VBA?

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.

What is expected array?

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.


1 Answers

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);

}
like image 87
Bohemian Avatar answered Sep 28 '22 01:09

Bohemian