Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

java 泛型设计这样有问题吗?

image.png

List<String>[] FounderComs = (List<String>[]) new ArrayList<String>[4];

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
// 在定义数组时,虽然也有new关键字,但作用就是给数组初始化一个大小。
// 不能理解为实例化操作,所以即使List是个接口,也是可以new的。
// 其标准可以归纳为: datatype [ ] arrayName = new datatype [size];
List<String> [] founderComs = new List [4];
// 这样写也可以。但一定不要加 <> ,加了 <> 就会认为你在new对象,而非定义数组大小。
founderComs = new ArrayList [4];
founderComs[0] = new ArrayList();
founderComs[1] = new ArrayList<>();
founderComs[2] = new ArrayList<String>();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...