简体   繁体   中英

Arrays.asList compilation problem

I've used Arrays.asList dozens if not hundreds of times without problem. All of a sudden previously compiling code is failing to compile after switching to NetBeans 6.9 from 6.8. Here's a few lines in question:

Node n = new NickNode(4,5);
Node m = new NonLocatableNode();
Node subclass = new NickSubclassNode();

List<Node> nodes = Arrays.asList(n,m,subclass);

The subclasses of node are not important; they compile fine. The line that gives me an error is the Arrays.asList line. I get the error

alt text http://grab.by/grabs/b553ffb898ca0874ef4741b8c87fc576.png

I have no idea where it's getting anything about a HelpCtx.Provider[]... Does anyone see anything wrong with this snippet?

Replacing the asList line with

List<Node> theNodes = new LinkedList<Node>();
theNodes.add(n);
theNodes.add(m);
theNodes.add(subclass);

works fine. But I prefer the shorter syntax of Arrays.asList

尝试这个

List<Node> nodes = Arrays.<Node>asList(n,m,subclass);    

Your error graphic is not showing up for me but it looks like a generics problem. Perhaps a compiler warning was switched into a compiler error when you moved from netbeans 6.8 to 6.9?

Try declaring your List as ...

List<? extends Node> nodes = Arrays.asList(n, m, subclass);

The wildcard syntax specifies that the list contains Nodes and anything that inherits from Node.

Yep you are right this is the bug in NetBeans 6.9 which is already reported.So hopefully it will get resolved soon. You can see that bug report here

it should be:

List<? extends Node> nodes = Arrays.asList(n,m,subclass);

bear in mind that :

List<Sub Class> is not a sub class for List<Parent Class>

they are different classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM