比较多个动态数组的每个元素

huangapple go评论69阅读模式
英文:

Comparing each element of multiple dynamics arrays

问题

现在,暂时不考虑时间复杂度,我需要比较列表中每个节点的数组[n]中的每个元素与其他数组中的所有元素。我该如何做呢?因为如果列表是固定大小的,我会像这样做(假设大小为3):

for (Array firstArray : lst.get(0).getArray()) {
   for (Array secondArray : lst.get(1).getArray()) {
      for (Array thirdArray : lst.get(2).getArray()) {
          // 进行某些操作
      }
   }
}

但是对于大小不固定的列表呢?我该如何实现我的目标呢?

英文:

I have a List of some typed object, where each node of the list has an array[n].

Now, leaving out the time complexity, for now I need to compare each element of the array with all the elements of others arrays inside the list. How can I do that? Because if list was fixed size, I would do something like that (suppose the size is 3):

for(Array firstArray : lst.get(0).getArray()){
   for(Array secondArray : lst.get(1).getArray()){
      for(Array thirdArray : lst.get(2).getArray()){
   //do something
                }
           }
   }

But with a list with no fixed size? How can achieve my goal?

答案1

得分: 1

在不确定数量的循环中听起来像是递归

void doAll(List<ArrayHolder> lst) {
    doAllFrom(lst, 0);
}
void doAllFrom(List<ArrayHolder> lst, int off) {
    if (off == lst.size()) {
        //做一些操作
    } else {
        ArrayHolder arrayHolder = lst.get(off);
        for (Array nthArray : arrayHolder.getArray()) {
            doAllFrom(lst, off+1);
        }
    }
}

显然你需要对nthArray做一些操作也许是将它添加到当前Array的线性列表中

void doAll(List<ArrayHolder> lst) {
    doAllFrom(lst, new ArrayHolder[lst.size()], 0);
}
void doAllFrom(List<ArrayHolder> lst, ArrayHolder[] holders, int off) {
    if (off == lst.size()) {
        //做一些操作
    } else {
        ArrayHolder arrayHolder = lst.get(off);
        for (Array nthArray : arrayHolder.getArray()) {
            holders[off] = nthArray;
            doAllFrom(lst, holders, off+1);
        }
    }
}
英文:

In indeterminate number of loops? Sounds like recursion:

void doAll(List&lt;ArrayHolder&gt; lst) {
    doAllFrom(lst, 0);
}
void doAllFrom(List&lt;ArrayHolder&gt; lst, int off) {
    if (off == lst.length()) {
        //do something
    } else {
        ArrayHolder arrayHolder = lst.get(off);
        for (Array nthArray : arrayHolder.getArray()) {
            doAllFrom(lst, off+1);
        }
    }
}

Obviously, you are going to need to do something with nthArray. Perhaps adding it to a linear list of current Arrays:

void doAll(List&lt;ArrayHolder&gt; lst) {
    doAllFrom(lst, new ArrayHolder[lst.size()], 0);
}
void doAllFrom(List&lt;ArrayHolder&gt; lst, ArrayHolder[] holders, int off) {
    if (off == lst.size()) {
        //do something
    } else {
        ArrayHolder arrayHolder = lst.get(off);
        for (Array nthArray : arrayHolder.getArray()) {
            holders[off] = nthArray;
            doAllFrom(lst, holders, off+1);
        }
    }
}

答案2

得分: 0

for (数组 first : lst.get(0).get数组()){
    for (int i : first){                     // i 是比较的第一个元素
        for (数组 second : lst.get(0).get数组()){
            for (int j : second){             // j 是比较的第二个元素
                if (i 比较 j)
                // 做一些操作
            }
        }
    }
}
这会完成你的任务但复杂度太高
考虑到数组的元素是整数
英文:
for(Array first : lst.get(0).getArray()){
    for(int i: first){                    //i is first element of comparison
       for(Array second : lst.get(0).getArray()){
          for(int j: second){             //j is second element of comparison
              if(i compare j)
              //do something
          }
       }
    }
}

this will do your job but complexity is too bad.
Considering elements of arrays are integers.

huangapple
  • 本文由 发表于 2020年9月30日 21:08:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64138285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定