Java中的ArrayList数组?

56

我想创建一个没有固定大小的多维数组。

我需要能够向其中添加String[2]类型的项。

我已经尝试查看了:

private ArrayList<String[]> action = new ArrayList<String[2]>();

但那不起作用。有人有其他的想法吗?

7个回答

82

应该是这样的

private ArrayList<String[]> action = new ArrayList<String[]>();
action.add(new String[2]);
...
您无法在泛型参数中指定数组的大小,只能稍后将特定大小的数组添加到列表中。这也意味着编译器无法保证所有子数组的大小相同,必须由您来确保。
更好的解决方案可能是将其封装在一个类中,在那里您可以将数组的统一大小作为类型不变式进行确保。

16

顺便说一下,您应该优先编写面向接口的代码。

private ArrayList<String[]> action = new ArrayList<String[]>();

应该是这样的

private List<String[]> action = new ArrayList<String[]>();

2
+1 同意。此外,通常应声明为 final。重新绑定容器变量的用例非常少。 - dsmith
3
好的,我会尽力为您翻译。以下是需要翻译的内容:@athena: 请参见Erich Gamma采访中的第一个问题及其答案:http://goo.gl/fz9G - missingfaktor

5

由于您的字符串数组的大小在编译时已固定,因此最好使用一个结构(例如 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,因为按照惯例,编程时应该针对接口进行编程。]

1
+1 我正要给出完全相同的答案。关于是否需要一个java.util.Pair类,已经有很多争论。我认为是需要的。 - dsmith
在一定程度上同意 - 你提供的Pair类是一个很好的开始,也是一个不错的泛化,因为我发现包含其他集合/数组的集合/数组是一种代码异味。然而,在这种情况下,OP可能将两个字符串配对在一起,它们之间可能有一些特殊的关系,在这种情况下,它们可能需要一个自己的类来指定这种关系(并包含特定于该对的方法)。当然,它可以是Pair<String,String>的子类。 - whaley
Java有java.util.Map.Pair。不需要自己定义。 - emory
@emory:它是 java.util.Map.Entry。它的名称表明了它的目的,即作为在 Map 中存储键和值的结构。它不应该被用作一般目的的结构来存储任何两个元素(这就是 Pair 的作用)。 - missingfaktor

4
ArrayList<String[]> action = new ArrayList<String[]>();

不需要String[2]


3

如已经被回答的那样,你可以按照@Péter Török所写的方式创建一个String数组的ArrayList;

    //Declaration of an ArrayList of String Arrays
    ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();

当给这个ArrayList分配不同的字符串数组时,每个字符串数组的长度都会不同。
在以下示例中,添加了4个不同的字符串数组,它们的长度各不相同。
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

还要注意,您可以按以下方式初始化一个新的String数组;
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[]>();

0

这个非常有效。

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

-1
  1. 创建ArrayList,如ArrayList action

    在JDK 1.5或更高版本中使用ArrayList <string[]>引用名称。

    在JDK 1.4或更低版本中使用ArrayList引用名称。

  2. 指定访问修饰符:

    • public,可以在任何地方访问
    • private,只能在类内部访问
    • protected,可以在类和不同包的子类中访问
  3. 然后指定它将被分配的引用

    action = new ArrayList<String[]>();
    
  4. 在JVM中,new关键字将为对象在运行时分配内存。

    您不应该在声明时分配值,因为您正在请求没有固定大小的值。

  5. 最后,您可以使用ArrayList中的add()方法。像这样使用

    action.add(new string[how much you need])
    

    它将在堆中分配特定的内存区域。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接