Array type expected [compilation error]

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 is this not working? I thought that everything that extends the array can be thought of as an array. Actually I can get lengthfrom an array.

+6
source share
1 answer

Actually there is no such class which extends Object[]; each array has a fixed type and its own class, for exampleMyClass[].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);

}
+5
source

Source: https://habr.com/ru/post/1017187/


All Articles