附加到二维数组

时间:2012-01-25 18:11:49

标签: java

应该是一个简单的,但我想添加说“一”,“一”,一个“或”两个“,”两个,“两个”到一个基于条件的二维数组

 String[][] headings = new String[][] { {
        "three",
        "three",
        "three"
               } };

2 个答案:

答案 0 :(得分:2)

我真的不明白你想如何对数组进行维度。如果添加后你将有6个元素,他们将如何放置? 1 x 6? 2 x 3?无论如何,您可以使用ArrayList<String[]>

ArrayList<String[]> headings = new ArrayList<String[]>();
headings.add(new String[] { "three", "three", "three" });

if(/* condition */)
    headings.add(new String[] { "two", "two", "two" });
else
    headings.add(new String[] { "one", "one", "one" });

答案 1 :(得分:1)

对于这个确切的任务,你可以做下一步。请注意,您无法动态调整数组大小,因此您应该关注启动时的大小或使用列表。

    String[][] headings = new String[][] { {
    "three",
    "three",
    "three"
           }, {} }; // note placeholder for to-be-added triplet.
     String val = condition ? "one": "two";
     headings[1] = new String[] {val, val, val};