Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:获取列表的所有连接<;列表<;字符串>&燃气轮机;_Java_Algorithm - Fatal编程技术网

Java:获取列表的所有连接<;列表<;字符串>&燃气轮机;

Java:获取列表的所有连接<;列表<;字符串>&燃气轮机;,java,algorithm,Java,Algorithm,我有一个列表 我需要得到第一维度上所有可能连接的列表 [ [1,2 ], [1] , [3,4] ] 应提供: [ 113, 114, 213, 214 ] 我正在尝试使用2个循环,好像应该可以 这就是我尝试过的: private static List<String> constructIndexes(List<List<String>> indexList){ List<String> index = new ArrayList&l

我有一个
列表
我需要得到第一维度上所有可能连接的列表

[ [1,2 ], [1] , [3,4] ]
应提供:

[ 113, 114, 213, 214 ]
我正在尝试使用2个循环,好像应该可以

这就是我尝试过的:

private static List<String> constructIndexes(List<List<String>> indexList){
    List<String> index = new ArrayList<String>();

    String v="";

    for (int i=0; i< indexList.size(); i++){
        List<String> l =  indexList.get(i);
        for (int j=0; j<l.size(); j++){
            if (index.size()>0){
                for (int k=0; k<index.size(); k++){
                    index.set(k, index.get(k)+l.get(j));
                    // System.out.println(">");
                }
            } else {
                index.add(l.get(j));
            }

        }
    }

    return index;
}
私有静态列表索引(列表索引列表){
列表索引=新的ArrayList();
字符串v=“”;
对于(int i=0;i对于(int k=0;k,由于外部列表(即列表列表)中的列表数直到运行时才知道,因此您无法预先知道循环数。在这种情况下,您需要递归解决方案:

public static void combine(List<List<String>> data, int[] pos, int n, List<String> res) {
    if (n == pos.length) {
        StringBuilder b = new StringBuilder();
        for (int i = 0 ; i != pos.length ; i++) {
            b.append(data.get(i).get(pos[i]));
        }
        res.add(b.toString());
    } else {
        for (pos[n] = 0 ; pos[n] != data.get(n).size() ; pos[n]++) {
            combine(data, pos, n+1, res);
        }
    }
}
publicstaticvoidcombine(列表数据,int[]pos,int n,列表res){
如果(n==位置长度){
StringBuilder b=新的StringBuilder();
for(int i=0;i!=pos.length;i++){
b、 追加(data.get(i).get(pos[i]);
}
res.add(b.toString());
}否则{
for(pos[n]=0;pos[n]!=data.get(n.size();pos[n]+){
联合收割机(数据、位置、n+1、res);
}
}
}
以下是如何称呼它:

List<List<String>> indexList = new ArrayList<List<String>>();
List<String> a = new ArrayList<String>();
a.add("1");
a.add("2");
List<String> b = new ArrayList<String>();
b.add("1");
List<String> c = new ArrayList<String>();
c.add("3");
c.add("4");
indexList.add(a);
indexList.add(b);
indexList.add(c);
List<String> res = new ArrayList<String>();
combine(indexList, new int[indexList.size()], 0, res);
for (String s : res) {
    System.out.println(s);
}
List indexList=new ArrayList();
列表a=新的ArrayList();
a、 添加(“1”);
a、 添加(“2”);
列表b=新的ArrayList();
b、 添加(“1”);
列表c=新的ArrayList();
c、 添加(“3”);
c、 添加(“4”);
添加(a);
增加(b);
增加(c);
List res=new ArrayList();
组合(indexList,新int[indexList.size()],0,res);
for(字符串s:res){
系统输出打印项次;
}

由于外部列表(即列表列表)中的列表数量直到运行时才知道,因此您无法预先知道循环的数量。在这种情况下,您需要递归解决方案:

public static void combine(List<List<String>> data, int[] pos, int n, List<String> res) {
    if (n == pos.length) {
        StringBuilder b = new StringBuilder();
        for (int i = 0 ; i != pos.length ; i++) {
            b.append(data.get(i).get(pos[i]));
        }
        res.add(b.toString());
    } else {
        for (pos[n] = 0 ; pos[n] != data.get(n).size() ; pos[n]++) {
            combine(data, pos, n+1, res);
        }
    }
}
publicstaticvoidcombine(列表数据,int[]pos,int n,列表res){
如果(n==位置长度){
StringBuilder b=新的StringBuilder();
for(int i=0;i!=pos.length;i++){
b、 追加(data.get(i).get(pos[i]);
}
res.add(b.toString());
}否则{
for(pos[n]=0;pos[n]!=data.get(n.size();pos[n]+){
联合收割机(数据、位置、n+1、res);
}
}
}
以下是如何称呼它:

List<List<String>> indexList = new ArrayList<List<String>>();
List<String> a = new ArrayList<String>();
a.add("1");
a.add("2");
List<String> b = new ArrayList<String>();
b.add("1");
List<String> c = new ArrayList<String>();
c.add("3");
c.add("4");
indexList.add(a);
indexList.add(b);
indexList.add(c);
List<String> res = new ArrayList<String>();
combine(indexList, new int[indexList.size()], 0, res);
for (String s : res) {
    System.out.println(s);
}
List indexList=new ArrayList();
列表a=新的ArrayList();
a、 添加(“1”);
a、 添加(“2”);
列表b=新的ArrayList();
b、 添加(“1”);
列表c=新的ArrayList();
c、 添加(“3”);
c、 添加(“4”);
添加(a);
增加(b);
增加(c);
List res=new ArrayList();
组合(indexList,新int[indexList.size()],0,res);
for(字符串s:res){
系统输出打印项次;
}

保留一些跟踪每个元素索引的索引计数器,然后执行传统的向左进位,与添加时相同,例如9+1=10(向左进位1,设置0)


保留一些跟踪每个元素索引的索引计数器,然后执行传统的向左进位,与添加时相同,例如9+1=10(向左进位1,设置0)


这对你有用吗

 private <T> List<T> getNthCombination(List<List<T>> lists, int n) {

    List<T> nthCombination = new ArrayList<T>(lists.size());

    int nCarry = n;
    for (List<? extends T> list: lists) {
         int lSize = list.size();
         nthCombination.add(list.get(nCarry % lSize));
         nCarry /= lSize;
    }

    return nthCombination;
}
private List-getn组合(列表列表,int-n){
List-nthCombination=newarraylist(lists.size());
int nCarry=n;

这对你有用吗

 private <T> List<T> getNthCombination(List<List<T>> lists, int n) {

    List<T> nthCombination = new ArrayList<T>(lists.size());

    int nCarry = n;
    for (List<? extends T> list: lists) {
         int lSize = list.size();
         nthCombination.add(list.get(nCarry % lSize));
         nCarry /= lSize;
    }

    return nthCombination;
}
private List-getn组合(列表列表,int-n){
List-nthCombination=newarraylist(lists.size());
int nCarry=n;

对于(ListSo…串联只能走一条路?(左-右,而不是右-左?)您可以这样说,您的代码中有什么不起作用?您得到了什么您不想要的?(错误,意外输出?)@Lattyware我同意ciril也应该把这些信息放进去,但是代码非常简单,很容易看出问题所在。在他的代码中,数组元素被添加到索引列表中,而不是被连接成字符串然后添加到索引列表中。这更像是制作一个好的索引的良好实践连接。所以…连接只能走一条路?(左-右,而不是右-左?)你说,你的代码中有什么不起作用?你得到了什么你不想要的?(错误,意外输出?)@Lattyware我同意ciril也应该把这些信息放进去,但是代码非常简单,很容易看出问题所在。在他的代码中,数组元素被添加到索引列表中,而不是被连接成字符串然后添加到索引列表中。这更像是制作一个好的索引的良好实践tion.nice thx,是的,我想知道复杂性,它是子列表大小的乘积,所以是的,(子列表的数量)循环是必要的nice thx,是的,我想知道复杂性,它是子列表大小的乘积,所以是(子列表的数量)循环是必要的Thx如果可能的话,我将在大集合上运行这里的2个答案,检查哪一个更快,我会说递归的,你的使用了很多checks@ciril我会说我的会更快,因为方法调用通常比循环中的检查更昂贵,直到[1..9]这两种实现几乎没有什么不同,完成这项工作大约需要500毫秒。好的(可能只需要计算相同或不同的输入100次)thx如果可能的话,我会在大集合上运行这里的2个答案,以检查哪一个更快,我会说递归的,你的使用了很多checks@ciril我会说我的会更快,因为方法调用通常比循环中的检查更昂贵两种实现都需要大约500毫秒才能完成工作,看起来几乎没有什么不同。好的(可能只需要计算相同或不同的输入100次)
 private <T> List<T> getNthCombination(List<List<T>> lists, int n) {

    List<T> nthCombination = new ArrayList<T>(lists.size());

    int nCarry = n;
    for (List<? extends T> list: lists) {
         int lSize = list.size();
         nthCombination.add(list.get(nCarry % lSize));
         nCarry /= lSize;
    }

    return nthCombination;
}