我需要將幾個字符串放入java數組中。在java數組中使用字符串
"Dog","Cat","Lion","Giraffe"
"Car","Truck","Boat","RV"
各
上面會數組中的1個鍵
array[0] = "Dog","Cat","Lion","Giraffe"
array[1] = "Car","Truck","Boat","RV"
不知道如何做到這一點,或者我應該用其他的東西比一個數組,以及如何獲得每個元素即array[0]"Lion"
由於
我需要將幾個字符串放入java數組中。在java數組中使用字符串
"Dog","Cat","Lion","Giraffe"
"Car","Truck","Boat","RV"
各
上面會數組中的1個鍵
array[0] = "Dog","Cat","Lion","Giraffe"
array[1] = "Car","Truck","Boat","RV"
不知道如何做到這一點,或者我應該用其他的東西比一個數組,以及如何獲得每個元素即array[0]"Lion"
由於
聲明數組這樣的:
String [][]array = {
{ "Dog","Cat","Lion","Giraffe"},
{"Car","Truck","Boat","RV"}
};
和使用的項目是這樣的:
array[0][0]; // this would be "Dog"
array[1][0]; // this would be "Car"
您可以使用一個多維數組:
String[][] something =
{
{ "hello", "kitties" },
{ "i", "am", "a", "pony" }
};
那麼你可以做到這一點通過聲明的地圖,像這樣Map<String, MySweetObject>
或創建一個List<String>
並把每個列表放入數組中。
你需要一個交錯數組,這是一個數組的數組:
String [][]array = { {"Dog","Cat","Lion","Giraffe"}, {"Car","Truck","Boat","RV"}};
然後,您可以訪問的內容,因爲這:
array[0] // will be the String array {"Dog","Cat","Lion","Giraffe"}
array[1] // will be the String array {"Car","Truck","Boat","RV"}
array[0][2] // Lion
array[1][0] // Car
他希望每個鍵多個項目,而不是個別的人 – Woot4Moo 2011-02-12 20:31:06