Welcome to OStack Knowledge Sharing 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...