我有很多Arraylists
个String
个对象,我需要连接这些值。
例如:
ArrayList finalList = new ArrayList();
ArrayList catMe = new ArrayList();
ArrayList x = new ArrayList();
x.add("Green");
x.add("Red");
ArrayList y = new ArrayList();
y.add(" Apple");
//......
catMe.add(x);
catMe.add(y);
concatContents(catMe); // Here i need to do
// some concatenation magic.
所以当打印finalList
时:
finalList.get(0) // should show > "Green Apple"
finalList.get(1) // should show > "Red Apple"
我知道如果只有两个列表X和Y,它看起来很容易......但是我需要n维度。说是否有第3个列表
ArrayList z= new ArrayList();
z.add(" USA");
z.add(" Canada");
catMe.add(z);
concatContents(catMe);
现在finalList应该显示
Green Apple USA
Green Apple Canada
Red Apple USA
Red Apple Canada
我需要递归吗?虽然无法思考如何实施!有没有java master有解决方案?
答案 0 :(得分:1)
这样的事情应该有效。 (我实际上没有编译它,为了简单起见将其写为sorta伪代码。处理泛型和正确的类型列表List>)
List<ArrayList> lists; // add all your lists to this list
ArrayList<String> final_list; // your final list of concatenations
for (int i=0; i<list1.size(); i++) {
String temp = ""
for (ArrayList current_list : lists) {
temp += " " +current_list.get(i);
}
final_list.add(temp);
}
编辑 - 好吧所以上面的代码有点愚蠢,我没有正确理解这个问题。现在,随着其他人发布了递归解决方案,我认为通过发布非递归工作解决方案可以获得回报。所以这是一个完全符合预期的工作
public static void main(String[] args) {
ArrayList<String> finalList = new ArrayList<String>();
ArrayList<String> x = new ArrayList<String>();
x.add("Green");
x.add("Red");
ArrayList<String> y = new ArrayList<String>();
y.add(" Apple");
ArrayList<String> z = new ArrayList<String>();
z.add(" USA");
z.add(" Canada");
finalList = concat(x, y, z);
System.out.println(finalList);
}
static ArrayList<String> concat(ArrayList<String>... lists) {
ArrayList<String> result = new ArrayList<String>();
for (ArrayList<String> list : lists) {
result = multiply(result, list);
}
return result;
}
static ArrayList<String> multiply(ArrayList<String> list1, ArrayList<String> list2) {
if (list2.isEmpty()) { return list1; }
if (list1.isEmpty()) { return list2; }
ArrayList<String> result = new ArrayList<String>();
for (String item2 : list2) {
for (String item1 : list1) {
result.add(item1 + item2);
}
}
return result;
}
答案 1 :(得分:1)
这是一个递归的答案。刚煮完,所以不保证质量...... :)
public ArrayList<String> concatLists(ArrayList<ArrayList<String>> list) {
ArrayList<String> catStrs = new ArrayList<String>();
int len = list.size();
if (len == 1) {
catStrs.addAll(list.get(0));
return catStrs;
}
ArrayList<String> myStrs = list.get(0);
ArrayList<ArrayList<String>> strs = new ArrayList<ArrayList<String>>();
strs.addAll(list.subList(1, len));
ArrayList<String> retStrs = concatLists(strs);
for (String str : myStrs) {
for (String retStr : retStrs) {
catStrs.add(str+retStr);
}
}
return catStrs;
}
答案 2 :(得分:1)
快速而肮脏的解决方案:
public class Lists {
public static void main(String[] args) {
List<List<String>> finalList = new ArrayList<List<String>>();
List<String> x = new ArrayList<String>();
x.add("Green");
x.add("Red");
x.add("Purple");
List<String> y = new ArrayList<String>();
y.add("Apple");
List<String> z = new ArrayList<String>();
z.add("USA");
z.add("UK");
z.add("France");
finalList.add(x);
finalList.add(y);
finalList.add(z);
for (String s: concat(finalList)) {
System.out.println(s);
}
}
private static List<String> concat(List<List<String>> inputList) {
if (inputList.size() == 1) {
return inputList.get(0);
} else {
List<String> newList = new ArrayList<String>();
List<String> prefixes = inputList.get(0);
for (String prefix : prefixes) {
for (String concat : concat(inputList.subList(1,inputList.size()))) {
newList.add(prefix + " " + concat);
}
}
return newList;
}
}
}
给出:
Green Apple USA
Green Apple UK
Green Apple France
Red Apple USA
Red Apple UK
Red Apple France
Purple Apple USA
Purple Apple UK
Purple Apple France
答案 3 :(得分:0)
这是我的简单实现:
List<ArrayList<String>> lists= new ArrayList<ArrayList<String>>();
ArrayList<String> final_list=new ArrayList<String>();;
int i=0;
while(true){
StringBuilder temp = new StringBuilder();
for(ArrayList<String> currentList:lists){
if(i<currentList.size()){
temp.append(currentList.get(i));
}
}
String row = temp.toString();
if(row.length()==0){
break;
} else{
final_list.add(row);
i++;
}
}