java实现:有n个char数组(List<char[]>),从每个数组取出一个char组成一个字符串,输出所有的组合
1个回答
展开全部
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String args[]) {
List<char[]> list = new ArrayList<>();
list.add(new char[]{'a', 'b', 'e'});
list.add(new char[]{'c'});
list.add(new char[]{'d', 'f'});
list.add(new char[]{'g', 'h'});
method("", list);
}
public static void method(String now, List<char[]> list) {
if (list == null || list.size() == 0) {
System.out.println(now);
return;
} else {
//复制一个副本
List<char[]> copy = copyList(list);
char [] charArr = copy.get(0);
copy.remove(0);
for (int i=0; i<charArr.length; i++) {
method (now + charArr[i], copy);
}
}
}
public static List<char[]> copyList(List<char[]> list) {
List<char[]> result = new ArrayList<>();
for (int i=0; i<list.size(); i++) {
result.add(list.get(i));
}
return result;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询