2014-05-05 35 views
0

我有打印的字符串ArrayList的值。從打印的ArrayList到ArrayList對象的轉換

[a, b, c, d]

這將是字符串格式。

例如,

String temp = "[a, b, c, d]"

我怎麼能轉換成String對象的ArrayList呢?

+2

那你試試?請顯示你的代碼。 – BetaRide

+0

爲什麼你有[]?你能告訴我這裏的含義嗎? –

+0

你的問題很混亂。 –

回答

0

非常簡單的代碼

String temp = "[a, b, c, d]"; 

// trim enclosing brackets and then split by comma and space 
String[] array = temp.substring(1, temp.length() - 1).split(", "); 

List<String> list = new ArrayList<String>(); 

for (String s : array) { 
    list.add(s); 
} 
+0

我假設沒有任何內置函數可以做到這一點 – user2324943

+0

對不起有沒有在JAVA世界的問題無窮:) – Braj

+0

你也可以幫我一件事,同樣,如果我有一個堆棧的內容在字符串中,並且想要重新轉換爲堆棧類型的對象,我該怎麼做?我將不得不以相反的順序讀取字符串? – user2324943