我有保存數組列表的列表,我希望它轉換成多維數組:如何將多維列表轉換爲java中的多維數組?
/*
The logs looks something like this
[Name: Foo, From: Somewhere, To: Somewhere]
[Name: Foo1, From: Somewhere1, To: Somewhere1]
[Name: Foo2, From: Somewhere2, To: Somewhere2]
...
*/
public static void main(String[] args) {
List<ArrayList<String>> items = new ArrayList<>();
for (String logs : Reader.read(App.purchaseLog, true) /* helper method */) {
String[] singleLog = convert(logs); /* helper method */
items.add(new ArrayList<String>());
for (String data : singleLog) {
String removeBrackets = data.replaceAll("\\[", "").replaceAll("\\]", "");
String[] splitSingleLog = convert(removeBrackets.split(","));
for (String element : splitSingleLog) {
String trimmedData = element.trim();
items.get(0).add(trimmedData.substring(trimmedData.indexOf(':') + 2, trimmedData.length()));
}
}
}
// But then how can i convert it to normal 2 dimensional array?
// i am expecting it to be like this
String[][] data = {
{"Foo", "Somewhere", "Somewhere"},
{"Foo1", "Somewhere1", "Somewhere1"},
{"Foo2", "Somewhere2", "Somewhere2"}
};
}
public static String[] convert(String... array) {
return array;
}
我怎麼能每次循環的ArrayList,改變成正常的排列? 我看到的ArrayList轉換到一個數組的幾個例子,但它主要是一維數組(Convert ArrayList<String> to String[] array)
誰downvoted並要求關閉?這個問題完全在SO的範圍內。 –
@ Jean-BaptisteYunès很明顯,我沒有倒下,關閉請求:但是......最後,這個問題實際上是基礎知識。我不覺得其他用戶寧願去關閉這個。 – GhostCat
我對此沒有收費......甚至基礎知識都值得回答,特別是對於初學者。 –