Java ArrayList 的数组?
我想创建一个没有固定大小的多维数组。
我需要能够向其中添加 String[2]
项目。
我尝试过查看:
private ArrayList<String[]> action = new ArrayList<String[2]>();
但这不起作用。还有人有其他想法吗?
I want to create a mutli dimensional array without a fixed size.
I need to be able to add items of String[2]
to it.
I have tried looking at:
private ArrayList<String[]> action = new ArrayList<String[2]>();
but that doesn't work. does anyone have any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
应该是
你不能在泛型参数中指定数组的大小,只能稍后将特定大小的数组添加到列表中。这也意味着编译器不能保证所有子数组的大小相同,必须由您来保证。
更好的解决方案可能是将其封装在一个类中,您可以在其中确保数组大小作为类型不变式的统一大小。
Should be
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.
顺便提一句。您应该更喜欢针对接口进行编码。
应该是
BTW. you should prefer coding against an Interface.
Should be
由于字符串数组的大小在编译时是固定的,因此最好使用仅要求两个字段的结构(如 Pair),从而避免数组方法可能出现的运行时错误。
代码:
由于 Java 不提供
Pair
类,因此您需要定义自己的类。然后将其用作:
[ 这里我使用
List
因为它被认为是对接口进行编程的良好实践。 ]Since the size of your string array is fixed at compile time, you'd be better off using a structure (like
Pair
) that mandates exactly two fields, and thus avoid the runtime errors possible with the array approach.Code:
Since Java doesn't supply a
Pair
class, you'll need to define your own.and then use it as:
[ Here I used
List
because it's considered a good practice to program to interfaces. ]不需要
String[2]
;Don't need
String[2]
;正如已经回答的那样,您可以创建一个字符串数组的 ArrayList,如 @Péter Török 所写;
当将不同的字符串数组分配给此ArrayList时,每个字符串数组的长度将不同。
在下面的示例中,添加了 4 个不同的字符串数组,它们的长度各不相同。
演示代码如下;
输出如下;
另请注意,您可以初始化一个新的 Sting 数组,如下所示;
所以这与以下相同;
我编写此演示只是为了表明没有 ArrayList 对象,所有元素都是对字符串数组的不同实例的引用,因此每个字符串数组的长度不必相同,也不必相同这很重要。
最后一点:最佳实践是在 List 接口中使用 ArrayList,而不是您在问题中使用的 ArrayList。
最好使用List接口,如下所示;
As already answered, you can create an ArrayList of String Arrays as @Péter Török written;
When assigning different String Arrays to this ArrayList, each String Array's length will be different.
In the following example, 4 different Array of String added, their lengths are varying.
The Demonstration code is as below;
And the output is as follows;
Also notify that you can initialize a new Array of Sting as below;
So this is same with;
I've written this demonstration just to show that no theArrayList object, all the elements are references to different instantiations of String Arrays, thus the length of each String Arrays are not have to be the same, neither it is important.
One last note: It will be best practice to use the ArrayList within a List interface, instead of which that you've used in your question.
It will be better to use the List interface as below;
这非常有效。
结果将是
This works very well.
Result will be
像
ArrayList操作
一样创建ArrayList。在 JDK 1.5 或更高版本中使用
ArrayList
引用名称。在 JDK 1.4 或更低版本中,使用
ArrayList
引用名称。指定访问说明符:
然后指定它将被分配的引用
JVM 中分配的引用
new
关键字将在运行时为该对象分配内存。您不应该在声明的地方分配值,因为您要求没有固定大小。
最后你可以使用ArrayList中的
add()
方法。使用类似它将在堆中分配特定的内存区域。
Create the ArrayList like
ArrayList action
.In JDK 1.5 or higher use
ArrayList <string[]>
reference name.In JDK 1.4 or lower use
ArrayList
reference name.Specify the access specifiers:
Then specify the reference it will be assigned in
In JVM
new
keyword will allocate memory in runtime for the object.You should not assigned the value where declared, because you are asking without fixed size.
Finally you can be use the
add()
method in ArrayList. Use likeIt will allocate the specific memory area in heap.