[英]Append to 2-dimensional array
應該很簡單,但我想根據條件在二維數組中添加“一個”,“一個”,“一個”或“兩個”,“兩個”,“兩個”
String[][] headings = new String[][] { {
"three",
"three",
"three"
} };
我不太了解如何確定數組的尺寸。 如果添加后您將擁有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" });
為此,您可以下一步。 請注意,您無法動態調整數組的大小,因此您應該在開始或使用列表時注意大小。
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};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.