我想创建一个没有固定大小的多维数组。
我需要能够向其中添加String[2]
类型的项。
我已经尝试查看了:
private ArrayList<String[]> action = new ArrayList<String[2]>();
但那不起作用。有人有其他的想法吗?
我想创建一个没有固定大小的多维数组。
我需要能够向其中添加String[2]
类型的项。
我已经尝试查看了:
private ArrayList<String[]> action = new ArrayList<String[2]>();
但那不起作用。有人有其他的想法吗?
应该是这样的
private ArrayList<String[]> action = new ArrayList<String[]>();
action.add(new String[2]);
...
您无法在泛型参数中指定数组的大小,只能稍后将特定大小的数组添加到列表中。这也意味着编译器无法保证所有子数组的大小相同,必须由您来确保。顺便说一下,您应该优先编写面向接口的代码。
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
,因为按照惯例,编程时应该针对接口进行编程。]java.util.Map.Entry
。它的名称表明了它的目的,即作为在 Map
中存储键和值的结构。它不应该被用作一般目的的结构来存储任何两个元素(这就是 Pair
的作用)。 - missingfaktorArrayList<String[]> action = new ArrayList<String[]>();
不需要String[2]
;
如已经被回答的那样,你可以按照@Péter Török所写的方式创建一个String数组的ArrayList;
//Declaration of an ArrayList of String Arrays
ArrayList<String[]> listOfArrayList = new ArrayList<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
new String[]{ str1, str2, str3,... }; // Assuming str's are String objects
String[] newStringArray = { str1, str2, str3 }; // Assuming str's are String objects
我编写了这个演示,只是为了展示ArrayList对象中的所有元素都是指向不同字符串数组实例的引用,因此每个字符串数组的长度不必相同,也不重要。
最后一条注意事项: 最好使用List接口作为ArrayList的替代,而不是你在问题中使用的那个。
建议使用以下代码:
//Declaration of an ArrayList of String Arrays
List<String[]> listOfArrayList = new ArrayList<String[]>();
这个非常有效。
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,如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])
它将在堆中分配特定的内存区域。