问题陈述的方式如下:
假设有4个字符串数组-[open,providers],[1,2],[yes,no],[oe,pr]
我想将4个数组添加到名为'series'的列表中。
所以系列列表现在类似于-[[[open,providers],[1,2],[是,否],[oe,pr]]
现在,我想遍历列表以从列表中的每个数组中获取第一个值,即将[open,1,yes,oe]和[providers,2,no,pr]作为两个单独的数组获取,或者列表。
该如何处理? 帮助代码段! 谢谢 !
答案 0 :(得分:1)
您应该执行以下操作。我在这里测试过,并且工作正常。只需改进println
即可显示您喜欢的文字。现在由您决定。学习代码。
import java.util.ArrayList;
public class Main {
public static void main(String args[]) {
String[] array1 = {"open", "providers"};
String[] array2 = {"1", "2"};
String[] array3 = {"yes", "no"};
String[] array4 = {"oe", "pr"};
ArrayList<String[]> result = myArrays(array1, array2, array3, array4);
for(int i = 0; i < result.size(); i++) {
for(int j = 0; j < result.get(i).length; j++) {
if(i == 0) {
System.out.println("Result of first array: " + result.get(i)[j]);
} else {
System.out.println("Result of second array: " + result.get(i)[j]);
}
}
}
}
public static ArrayList<String[]> myArrays(String[] array1, String[] array2, String[] array3, String[] array4) {
String[][] series = new String[4][];
series[0] = array1;
series[1] = array2;
series[2] = array3;
series[3] = array4;
String[] firstResult = new String[4];
String[] secondResult = new String[4];
for(int i = 0; i < series.length; i++) {
for(int j = 0; j < series[i].length; j++) {
if(j == 0) {
firstResult[i] = series[i][j];
} else {
secondResult[i] = series[i][j];
}
}
}
ArrayList<String[]> finalResult = new ArrayList<String[]>();
finalResult.add(firstResult);
finalResult.add(secondResult);
return finalResult;
}
}
答案 1 :(得分:0)
String[] arr1 = {"open","providers"};
String[] arr2 = {"1","2"};
String[] arr3 = {"yes","no"};
List<String[]> addList = new ArrayList<String[]>();
for (int n = 0; n < arr1.length; n++) {
String[] rowArray = { arr1[n], arr2[n],
arr3 [n]};
addList.add(rowArray);
}
这是提供的,数组的长度相似,可以很容易地容纳它!