Java ArrayList 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3642205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java ArrayList of Arrays?
提问by Rumpleteaser
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.
我需要能够String[2]
向它添加项目。
I have tried looking at:
我试过看:
private ArrayList<String[]> action = new ArrayList<String[2]>();
but that doesn't work. does anyone have any other ideas?
但这不起作用。有没有人有其他想法?
采纳答案by Péter T?r?k
Should be
应该
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.
更好的解决方案可能是将其封装在一个类中,您可以在其中确保数组大小的统一作为类型不变量。
回答by Koerr
ArrayList<String[]> action = new ArrayList<String[]>();
Don't need String[2]
;
不需要String[2]
;
回答by Adalarasan Sachithanantham
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:
- public, can be accessed anywhere
- private, accessed within the class
- protected, accessed within the class and different package subclasses
Then specify the reference it will be assigned in
action = new ArrayList<String[]>();
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 likeaction.add(new string[how much you need])
It will allocate the specific memory area in heap.
创建 ArrayList 像
ArrayList action
。在 JDK 1.5 或更高版本中使用
ArrayList <string[]>
引用名称。在 JDK 1.4 或更低版本中使用
ArrayList
引用名称。指定访问说明符:
- 公开,可以在任何地方访问
- 私有,在类内访问
- 受保护,在类和不同的包子类中访问
然后指定它将被分配的引用
action = new ArrayList<String[]>();
在 JVM 中,
new
关键字将在运行时为对象分配内存。您不应该在声明的地方分配值,因为您要求没有固定大小。
最后你可以使用
add()
ArrayList 中的方法。使用喜欢action.add(new string[how much you need])
它将在堆中分配特定的内存区域。
回答by daniel
BTW. you should prefer coding against an Interface.
顺便提一句。您应该更喜欢针对接口进行编码。
private ArrayList<String[]> action = new ArrayList<String[]>();
Should be
应该
private List<String[]> action = new ArrayList<String[]>();
回答by missingfaktor
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.
由于您的字符串数组的大小在编译时是固定的,您最好使用一个结构(如Pair
)来强制要求恰好两个字段,从而避免使用数组方法可能出现的运行时错误。
Code:
代码:
Since Java doesn't supply a Pair
class, you'll need to define your own.
由于 Java 不提供Pair
类,因此您需要定义自己的类。
class Pair<A, B> {
public final A first;
public final B second;
public Pair(final A first, final B second) {
this.first = first;
this.second = second;
}
//
// Override 'equals', 'hashcode' and 'toString'
//
}
and then use it as:
然后将其用作:
List<Pair<String, String>> action = new ArrayList<Pair<String, String>>();
[ Here I used List
because it's considered a good practice to program to interfaces. ]
[ 我使用这里List
是因为它被认为是编程接口的好习惯。]
回答by Levent Divilioglu
As already answered, you can create an ArrayList of String Arrays as @Péter T?r?k written;
正如已经回答的那样,您可以创建一个字符串数组的 ArrayList,如@Péter T?r?k 所写;
//Declaration of an ArrayList of String Arrays
ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();
When assigning different String Arrays to this ArrayList, each String Array's length will be different.
当给这个ArrayList 分配不同的String Array 时,每个String Array 的长度都会不同。
In the following example, 4 different Array of String added, their lengths are varying.
在以下示例中,添加了 4 个不同的 String Array,它们的长度各不相同。
String Array #1: len: 3
String Array #2: len: 1
String Array #3: len: 4
String Array #4: len: 2
The Demonstration code is as below;
演示代码如下;
import java.util.ArrayList;
public class TestMultiArray {
public static void main(String[] args) {
//Declaration of an ArrayList of String Arrays
ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();
//Assignment of 4 different String Arrays with different lengths
listOfArrayList.add( new String[]{"line1: test String 1","line1: test String 2","line1: test String 3"} );
listOfArrayList.add( new String[]{"line2: test String 1"} );
listOfArrayList.add( new String[]{"line3: test String 1","line3: test String 2","line3: test String 3", "line3: test String 4"} );
listOfArrayList.add( new String[]{"line4: test String 1","line4: test String 2"} );
// Printing out the ArrayList Contents of String Arrays
// '$' is used to indicate the String elements of String Arrays
for( int i = 0; i < listOfArrayList.size(); i++ ) {
for( int j = 0; j < listOfArrayList.get(i).length; j++ )
System.out.printf(" $ " + listOfArrayList.get(i)[j]);
System.out.println();
}
}
}
And the output is as follows;
输出如下;
$ line1: test String 1 $ line1: test String 2 $ line1: test String 3
$ line2: test String 1
$ line3: test String 1 $ line3: test String 2 $ line3: test String 3 $ line3: test String 4
$ line4: test String 1 $ line4: test String 2
Also notify that you can initialize a new Array of Sting as below;
还要通知您可以初始化一个新的 Sting 数组,如下所示;
new String[]{ str1, str2, str3,... }; // Assuming str's are String objects
So this is same with;
所以这与;
String[] newStringArray = { str1, str2, str3 }; // Assuming str's are String objects
I've written this demonstration just to show that no theArrayListobject, 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.
我写这个演示只是为了表明没有ArrayList对象,所有元素都是对 String Arrays 不同实例的引用,因此每个 String Arrays 的长度不必相同,也不重要。
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.
最后一个注意事项:在 List 接口中使用 ArrayList 将是最佳实践,而不是您在问题中使用的那个。
It will be better to use the List interface as below;
最好使用如下的 List 接口;
//Declaration of an ArrayList of String Arrays
List<String[]> listOfArrayList = new ArrayList<String[]>();
回答by ZBorkala
This works very well.
这非常有效。
ArrayList<String[]> a = new ArrayList<String[]>();
a.add(new String[3]);
a.get(0)[0] = "Zubair";
a.get(0)[1] = "Borkala";
a.get(0)[2] = "Kerala";
System.out.println(a.get(0)[1]);
Result will be
结果将是
Borkala