例如,我需要将几个字符串放入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"
由于
答案 0 :(得分:2)
像这样声明数组:
String [][]array = {
{ "Dog","Cat","Lion","Giraffe"},
{"Car","Truck","Boat","RV"}
};
并使用以下项目:
array[0][0]; // this would be "Dog"
array[1][0]; // this would be "Car"
答案 1 :(得分:1)
您可以使用多维数组:
String[][] something =
{
{ "hello", "kitties" },
{ "i", "am", "a", "pony" }
};
答案 2 :(得分:0)
您可以通过声明类似Map<String, MySweetObject>
的地图或创建List<String>
并将每个列表放入数组中来实现。
答案 3 :(得分:0)
你需要一个锯齿状数组,这是一个数组数组:
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