如有数组[["白色","黑色"],["64GB","128GB"],["中国移动","中国联通"]]
要拼接返回一个数组
如["白色/64GB/中国移动","黑色/64GB/中国移动","白色/128GB/中国移动","黑色/128GB/中国移动","白色/64GB/中国联通","黑色/64GB/中国联通","白色/128GB/中国联通","黑色/128GB/中国联通"]
问题是 数组[["白色","黑色"],["64GB","128GB"],["中国移动","中国联通"]] 不是固定的的 可能只有一个数组 可能有两个数组 可能有三个数组 可能有四个数组
数组里的值也不固定 ,可能只有一个,可能有5个
有大神在吗?
public class test {
public static List> source;
public static void main(String[] args) {
source = new ArrayList<>();
List<String> a = new ArrayList<String>();
a.add("黑色");
a.add("白色");
List<String> b = new ArrayList<String>();
b.add("64G");
b.add("128G");
List<String> c = new ArrayList<String>();
c.add("中国联通");
c.add("中国移动");
source.add(a);
source.add(b);
source.add(c);
ArrayList<String> result = new ArrayList<>();
recursion(result, source.get(0), 0, "");
System.out.println(result);
}
public static void recursion(List<String> result, List<String> para, int num, String choose) {
for (int i = 0; i < para.size(); i++) {
if (source.size() == num + 1) {
result.add(choose + "/" + para.get(i));
} else {
recursion(result, source.get(num + 1), num + 1, choose + "/" + para.get(i));
}
}
}
}
String[] str1 = new String[]{"白色","黑色"};
String[] str2 = new String[]{"64g","128g"};
String[] str3 = new String[]{"移动","联通"};
StringBuilder sb = null;
List<String> lis = new ArrayList<String>();
for(String s1 : str1){
for(String s2 : str2){
for(String s3 : str3){
sb = new StringBuilder();
sb.append(s1).append(s2).append(s3);
lis.add(sb.toString());
}
}
}
for(String str : lis){
System.out.println(str);
}
public static void main(String[] args) {
String arr[] = {"12g","32g","64g"};
String att [] = {"黑色","白色","黄色"};
String att1 [] = {"小","中","大"};
List list = new ArrayList();
list.add(arr);
list.add(att);
list.add(att1);
List list1 = test(list, new ArrayList(),arr, "");
System.out.println(list1);
}
public static List<String> test(List<String[]> list,List<String> result, String[] arr, String str)
{
for (int i = 0; i < list.size(); i++)
{
//取得当前的数组
if (i == list.indexOf(arr))
{
//迭代数组
for (String st : arr)
{
st = str+"#" + st;
if (i < list.size() - 1)
{
test(list,result, list.get(i + 1), st);
}
else if (i == list.size() - 1)
{
result.add(st);
}
}
}
}
return result;
}
}
public static void main(String[] args) {
List list = new ArrayList();
list.add(new String[]{"白色","黑色"});
list.add(new String[]{"64GB","128GB"});
list.add(new String[]{"中国移动","中国联通"});
List<String> resultList = new ArrayList<>();
for(int a = 0;a < list.get(0).length;a++){
for(int b = 0;b < list.get(1).length;b++){
for(int c = 0;c < list.get(2).length;c++){
resultList.add(list.get(0)[a]+"/"+list.get(1)[b]+"/"+list.get(2)[c]);
}
}
}
System.out.println(resultList);
}
public class ThirdpartyApplicationTests {
static ArrayList source;
public static void main(String[] args) {
source = new ArrayList<>();
String[] a = {"黑色","白色"};
String[] b = {"64G","128G"};
String[] c = {"中国联通","中国移动"};
source.add(a);
source.add(b);
source.add(c);
ArrayList<String> result = new ArrayList<>();
recursion(result,source.get(0),0,"");
System.out.println(result);
}
public static void recursion(List result,String[] para,int num,String choose){
for (int i = 0; i < para.length; i++) {
if (source.size() == num+1){
result.add(choose+"/"+para[i]);
}else {
recursion(result,source.get(num+1),num+1,choose+"/"+para[i]);
}
}
}
}
consle:[/黑色/64G/中国联通, /黑色/64G/中国移动, /黑色/128G/中国联通, /黑色/128G/中国移动, /白色/64G/中国联通, /白色/64G/中国移动, /白色/128G/中国联通, /白色/128G/中国移动]
第一步,把数据记进数据库3张表。
第二部,3表联合查询得出结果。
完事。