[英]Converting two arraylist to a 2D string
While developing an application i come across a situation as below: 在开发应用程序时,我遇到以下情况:
I have two Arralylists - list1, list2 which have values like: 我有两个Arralylists-list1,list2,其值类似于:
list1 = [district1, district2, district3, district4, district5]
list2 = [service1, service2, service3, sevice4, service5]
Now i want to store the values in a third arraylist, list3 which will be like: 现在,我想将值存储在第三个arraylist中,list3类似于:
list3 = [district1, service1, district2, service2, district3, service3, district4, service4, district5, service 5]
Now i need to convert list3 to a two dimensional String (values[][]) which will be like: 现在,我需要将list3转换为二维字符串(values [] []),如下所示:
values = [ [district1, service1], [district2, service2], [district3, service3], [district4, service4], [district5, service 5]]
Basically i need values[][]. 基本上我需要值[] []。 I am not sure whether list3 is required or not. 我不确定list3是否是必需的。 I spend the whole day working on this, bt couldnot figure out a solution. 我花了一整天的时间对此进行研究,bt无法找到解决方案。 Would really appreciate someone's help. 非常感谢某人的帮助。 Thanks in advance. 提前致谢。
Assuming the lists contain String
and list1.size() == list2.size()
: 假设列表包含String
和list1.size() == list2.size()
:
String[][] values = new String[list1.size()][2];
for (int i = 0; i < list1.size(); i++) {
values[0] = list1.get(i);
values[1] = list2.get(i);
}
If list1 and list2 have same # of elements then something like following code should work for you: 如果list1和list2具有相同的元素数,则类似以下代码的代码应为您工作:
String[][] list3 = new String[list1.size()][2];
for (int i=0; i<list1.size(); i++) {
list3[i][0] = list1.get(i);
list3[i][1] = list2.get(i);
}
Assuming both the lists are of same size, following code would help: 假设两个列表大小相同,以下代码将有所帮助:
public String[][] merge(List<String> list1, List<String> list2) {
String[][] values = new String[list1.size()][];
for(int i=0 ; i< list1.size() ; i++){
values[i] = new String[2];
values[i][0] = list1.get(i);
values[i][1] = list2.get(i);
}
return values;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.