英文:
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<ArrayHolder> lst) {
doAllFrom(lst, 0);
}
void doAllFrom(List<ArrayHolder> 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 Array
s:
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()) {
//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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论