gpt4 book ai didi

java - 如何将字符串数组放入字符串数组的数组中?

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:42 24 4
gpt4 key购买 nike

我有 4 个这样的字符串数组:

String[] array1  = new String{"there",  "there2", "there3", "there4"};
String[] array2 = new String{"here","here2","here3"};
String[] array3 = new String{"hi","hi2"};
String[] array4 = new String{"blah","blah2","blah3"};

我想将它们放入一个看起来像这样的数组中:

   Array myArray =  [{"there",  "there2", "there3", "there4"},
{"here","here2","here3"},{"hi","hi2"},
{"blah","blah2","blah3"}];

然后就可以像这样访问其中的元素了:

myArray[0][1] would equal "there2"
myArray[1][2] would equal "here3"

希望这是有道理的,我该怎么做呢?

我试过像这样制作一个 ArrayList 然后添加它们,但它似乎不起作用

ArrayList<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(myArray);

最佳答案

很简单。检查这个link有关创建、初始化和访问数组的更多信息。

String[] array1 = new String[]{"there", "there2", "there3", "there4"};
String[] array2 = new String[]{"here", "here2", "here3"};
String[] array3 = new String[]{"hi", "hi2"};
String[] array4 = new String[]{"blah", "blah2", "blah3"};

String[][] allArray = {
array1,
array2,
array3,
array4
};

关于java - 如何将字符串数组放入字符串数组的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27432520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com