[英]Java ArrayList of Arrays?
我想创建一个没有固定大小的mutli维数组。
我需要能够添加String[2]
项目。
我试过看:
private ArrayList<String[]> action = new ArrayList<String[2]>();
但这不起作用。 有没有人有任何其他想法?
应该
private ArrayList<String[]> action = new ArrayList<String[]>();
action.add(new String[2]);
...
您不能在generic参数中指定数组的大小,只能稍后将特定大小的数组添加到列表中。 这也意味着编译器无法保证所有子阵列的大小相同,必须由您确保。
更好的解决方案可能是将其封装在一个类中,您可以在其中确保数组的统一大小作为类型不变量。
BTW。 你应该更喜欢对接口进行编码。
private ArrayList<String[]> action = new ArrayList<String[]>();
应该
private List<String[]> action = new ArrayList<String[]>();
由于字符串数组的大小在编译时是固定的,因此最好使用一个强制要求两个字段的结构(如Pair
),从而避免使用数组方法可能出现的运行时错误。
码:
由于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'
//
}
然后将其用作:
List<Pair<String, String>> action = new ArrayList<Pair<String, String>>();
[这里我使用List
因为它被认为是编程接口的好习惯。 ]
正如已经回答的那样,您可以创建一个字符串数组的ArrayList,如@PéterTörök所写;
//Declaration of an ArrayList of String Arrays
ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();
为此ArrayList分配不同的String Arrays时,每个String Array的长度都不同。
在以下示例中,添加了4个不同的String数组,其长度各不相同。
String Array #1: len: 3
String Array #2: len: 1
String Array #3: len: 4
String Array #4: len: 2
演示代码如下;
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();
}
}
}
输出如下;
$ 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
还要通知您可以初始化一个新的Sting数组,如下所示;
new String[]{ str1, str2, str3,... }; // Assuming str's are String objects
所以这是一样的;
String[] newStringArray = { str1, str2, str3 }; // Assuming str's are String objects
我写这个演示只是为了表明没有ArrayList对象,所有元素都是对String Arrays的不同实例的引用,因此每个String Arrays的长度不必相同,也不重要。
最后一点 :最佳实践是在List接口中使用ArrayList,而不是您在问题中使用的那个。
最好使用List接口,如下所示;
//Declaration of an ArrayList of String Arrays
List<String[]> listOfArrayList = new ArrayList<String[]>();
ArrayList<String[]> action = new ArrayList<String[]>();
不需要String[2]
;
这非常有效。
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]);
结果将是
Borkala
像ArrayList action
一样创建ArrayList action
。
在JDK 1.5或更高版本中使用ArrayList <string[]>
引用名称。
在JDK 1.4或更低版本中使用ArrayList
引用名称。
指定访问说明符:
然后指定它将被分配的引用
action = new ArrayList<String[]>();
在JVM中, new
关键字将在运行时为对象分配内存。
您不应该在声明的位置分配值,因为您要求的是没有固定大小的值。
最后,您可以在ArrayList中使用add()
方法。 用得像
action.add(new string[how much you need])
它将在堆中分配特定的内存区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.