阵列Java的ArrayList的? [英] Java ArrayList of Arrays?
问题描述
我想没有一个固定的大小来创建一座耗资维数组。
I want to create a mutli dimensional array without a fixed size.
我需要能够给项目添加字符串[2]
来了。
I need to be able to add items of String[2]
to it.
我曾尝试寻找:
private ArrayList<String[]> action = new ArrayList<String[2]>();
但是,这并不工作。没有任何人有任何其他的想法?
but that doesn't work. does anyone have any other ideas?
推荐答案
应
private ArrayList<String[]> action = new ArrayList<String[]>();
action.add(new String[2]);
...
可以不通用参数中指定的数组的大小,只添加特定尺寸的阵列以列表以后。这也意味着,编译器不能保证所有的子阵列是同样大小的,必须由你来保证。
You can't specify the size of the array within the generic parameter, only add arrays of specific size to the list later. This also means that the compiler can't guarantee that all sub-arrays be of the same size, it must be ensured by you.
有一个更好的解决方案可能是一类,在那里你可以保证数组的大小均匀为不变型封装内这个。
A better solution might be to encapsulate this within a class, where you can ensure the uniform size of the arrays as a type invariant.
这篇关于阵列Java的ArrayList的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!