有没有更好的方法来实现这些嵌套循环? [英] Is there a better way to implement those nested loops?

查看:167
本文介绍了有没有更好的方法来实现这些嵌套循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有三个数组:

  char [] one = {'a','b','c'} ; 
char [] two = {'1','2','3'};
char [] three = {'x','y','z'};

如果我想像这样打印它们:

  a 1 x 
a 1 y
a 1 z

a 2 x
a 2 y
... ..
c 3 z

我必须创建三个嵌套循环; (char i:one)
(char j:two)
for(char k)

  :three)
// .....

假设我有7个数组,这将是7个嵌套循环。



有没有更好的方法来做到这一点或API可能?我知道你可能会问为什么你做这样的事情,这只是思考的问题。 解决方案

使用一系列索引。更新他们像汽车里程表:最不重要的指标,然后如果溢出设置为零,并继续下一个...当没有溢出,更新完成。继续打印。当最后一个索引溢出时,打印完成。

  //接受一组字符数组并打印一个嵌套
//的内容。
static void print(char [] [] a){
int n_arrays = a.length;
int [] indices = new int [n_arrays]; //所有由java
设置为0 //递减以使第一个增量为全零。避免测试空数组。
indices [n_arrays - 1] = -1;
for(int j = 0 ;;){
//递增索引。
for(j = n_arrays-1; j> = 0; j--){
if(++ indices [j]> = a [j] .length){
索引[j] = 0;
}
else {
break; //没有溢出递增完成。
}
}
if(j <0){
break; //最后一个索引溢出我们完成了。
}
//打印。
for(int i = 0; i< n_arrays; i ++){
System.out.print(a [i] [indices [i]]);
}
System.out.println();
}
}

// Varargs版本。
static void printArgs(char [] ... a){
print(a);


static char [] one = {'a','b','c'};
static char [] two = {'1','2','3'};
static char [] three = {'x','y','z'};

public static void main(String [] a){
print(new char [] [] {one,two,three});
printArgs(一,二,三);
}


Suppose having three arrays:

char [] one = {'a','b','c'};
char [] two = {'1','2','3'};
char [] three = {'x','y','z'};

If I want to print them like this:

a 1 x
a 1 y
a 1 z

a 2 x
a 2 y
.....
c 3 z

I must create three nested loops;

for(char i : one)
  for(char j : two)
    for(char k : three)
       //.....

Suppose if I have 7 arrays, it will be 7 nested loops.

Is there a better way to do that or an API maybe? I know you're probably asking why you do such a thing, It's just the mater of thinking.

解决方案

The trick is to use an array of indices. Update them like the odometer in a car: least significant index then if overflow set that to zero and move on to the next... When no overflow, the update is done. Go ahead and print. When there's an overflow of the last index, printing is done.

// Accept an array of character arrays and print a nest
// of their contents.
static void print(char [] [] a) {
    int n_arrays = a.length;
    int [] indices = new int[n_arrays];  // All set to 0 by java
    // Decrement so that first increment is to all zeros. Avoids test for empty arrays.
    indices[n_arrays - 1] = -1; 
    for (int j = 0;;) {
        // Increment indices.
        for (j = n_arrays - 1; j >= 0; j--) {
            if (++indices[j] >= a[j].length) {
                indices[j] = 0;
            }
            else {
                break;  // No overflow. Increment is complete.
            }
        }
        if (j < 0) {
            break; // Last index overflowed.  We're done.
        }
        // Print.
        for (int i = 0; i < n_arrays; i++) {
            System.out.print(a[i][indices[i]]);
        }
        System.out.println();
    }
}

// Varargs version.
static void printArgs(char [] ... a) {
    print(a);
}

static char [] one = {'a','b','c'};
static char [] two = {'1','2','3'};
static char [] three = {'x','y','z'};

public static void main(String[] a) {
    print(new char [] [] { one, two, three } );
    printArgs(one, two, three);
}

这篇关于有没有更好的方法来实现这些嵌套循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆