在 Java 中推广 for 循环

发布于 2024-12-02 11:19:22 字数 950 浏览 0 评论 0原文

我真的不知道如何描述我的问题,所以我只展示一个代码示例:

int[][][] mat;
int n;
int specificValue;

for (int i = 0; i < n; i++) {
    if(mat[i][n-i][3] != specificValue) {
        doStuff();
    }
}

我正在 3d 数组中查找整数值。对于每个字段,我必须使用其中一个:

  • 从零运行到 n 的计数器
  • 从 n 到零运行的计数器
  • 固定值

所以我尝试构建一个方法,该方法可以使我免于编写此 for 循环大约 20 次,但我失败了,所以这就是我需要帮助的地方。我的想法是这样的:

search(Loop.UP, Loop.DOWN, Loop.FIXED);

其中“Loop”将是一个代表我的可能性之一的枚举,但我不知道如何实现它,或者这在Java中是否可能,而无需对每种可能的组合进行硬编码。

希望你能帮忙:)


好吧,更具体一点...通过我的设置,我正在通过这个 3d 数组绘制一个向量,具体来说是一条对角线,我想知道这个向量上的值是什么,并且 only< /strong> 在此向量上。

而且因为绘制这样的向量的可能性不止一种,所以我希望有一种更通用的方法来获取这些值。我的搜索可以很简单,就像

search(Loop.UP, Loop.FIXED, Loop.FIXED);   // one plane

一个带有一个计数器的简单 for 循环一样简单,但也

search(Loop.DOWN, Loop.UP, Loop.UP);   // through all three planes

I don't really know how to describe my issue, so I'll just show a code example:

int[][][] mat;
int n;
int specificValue;

for (int i = 0; i < n; i++) {
    if(mat[i][n-i][3] != specificValue) {
        doStuff();
    }
}

I'm looking up Integer values in a 3d-array. For each field I have to use one of these:

  • a counter running from zero to n
  • a counter running from n to zero
  • a fixed value

So i've tried to build a Method that would save me from writing this for loop about 20 times, but i failed, so that's where i need help. My idea was something like this:

search(Loop.UP, Loop.DOWN, Loop.FIXED);

where "Loop" would be an enum representing one of my possibilities, but I don't know how to implement it or if this is even possible in Java without hardcoding every possible combination.

Hope you can help :)


OK, more specific... with my setup i'm drawing a vector through this 3d-array, a diagonal to be specific and i want to know what values are on this vector and only on this vector.

And because there is more than one possibility to draw such a vector, i'd like to have a more general method to get to the values. My search could just be as simple as

search(Loop.UP, Loop.FIXED, Loop.FIXED);   // one plane

which would be a simple for-loop with one counter, but also

search(Loop.DOWN, Loop.UP, Loop.UP);   // through all three planes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫﹏色ふ单纯 2024-12-09 11:19:22
Path pathX = new Path.Up();
Path pathY = new Path.Down(n);
Path pathZ = new Path.Fixed(3);

for (int i = 0; i < n; i++) {
    if(mat[pathX.pos(i)][pathY.pos(i)][pathZ.pos(i)] != specificValue) {
        doStuff();  
    }
}

我没有使用枚举,

public interface Path {
    public int pos(int i);

    public static class Up implements Path {
         @Override public int pos(int i) { return i; }
    }

    public static class Down implements Path {
         private int n;
         public Down(int n) { this.n = n; }
         @Override public int pos(int i) { return n - i - 1; }
    }

    public static class Fixed implements Path {
         private int v;
         public Down(int v) { this.v = v; }
         @Override public int pos(int i) { return v; }
    }

因为 Down 取决于 nFixed 取决于某个值。

Path pathX = new Path.Up();
Path pathY = new Path.Down(n);
Path pathZ = new Path.Fixed(3);

for (int i = 0; i < n; i++) {
    if(mat[pathX.pos(i)][pathY.pos(i)][pathZ.pos(i)] != specificValue) {
        doStuff();  
    }
}

where

public interface Path {
    public int pos(int i);

    public static class Up implements Path {
         @Override public int pos(int i) { return i; }
    }

    public static class Down implements Path {
         private int n;
         public Down(int n) { this.n = n; }
         @Override public int pos(int i) { return n - i - 1; }
    }

    public static class Fixed implements Path {
         private int v;
         public Down(int v) { this.v = v; }
         @Override public int pos(int i) { return v; }
    }

I didn't use an Enum because Down depends on n and Fixed on some value.

征棹 2024-12-09 11:19:22

您可以使用索引列表。在 Python 中,它可能看起来像这样:

LOOP_UP = 20
LOOP_DOWN = LOOP_UP + 20
LOOP_FIXED = LOOP_DOWN + 20

def indexesOfLoopType(loopType, val):
    if loopType == LOOP_UP:
        return range(val)    
    if loopType == LOOP_DOWN:
        return range(val, 0)
    return [val] * val # make a list [val, val, val... ] of length val

def search(loopFirstIndex, loopSecondIndex, loopThirdIndex):
    # use indexesOfLoopType() to get lists of indexes
    # use those lists to iterate over mat

You could use a list of indices. In Python, it could look something like this:

LOOP_UP = 20
LOOP_DOWN = LOOP_UP + 20
LOOP_FIXED = LOOP_DOWN + 20

def indexesOfLoopType(loopType, val):
    if loopType == LOOP_UP:
        return range(val)    
    if loopType == LOOP_DOWN:
        return range(val, 0)
    return [val] * val # make a list [val, val, val... ] of length val

def search(loopFirstIndex, loopSecondIndex, loopThirdIndex):
    # use indexesOfLoopType() to get lists of indexes
    # use those lists to iterate over mat
马蹄踏│碎落叶 2024-12-09 11:19:22

目前我能想到的最好的办法是:

static final int UP = -2, DOWN = -1;

static void loop1(int[][][] A, int t1, int t2, int t3) {
    switch (t1) {
    case UP:
        for (int i = 0; i < A.length; i++)
            loop2(A[i], t2, t3);
        break;
    case DOWN:
        for (int i = A.length - 1; i >= 0; i--)
            loop2(A[i], t2, t3);
        break;
    default:
        loop2(A[t1], t2, t3);
    }
}

static void loop2(int[][] A, int t2, int t3) {
    switch (t2) {
    case UP:
        for (int i = 0; i < A.length; i++)
            loop3(A[i], t3);
        break;
    case DOWN:
        for (int i = A.length - 1; i >= 0; i--)
            loop3(A[i], t3);
        break;
    default:
        loop3(A[t2], t3);
    }
}

static void loop3(int[] A, int t3) {
    switch (t3) {
    case UP:
        for (int i = 0; i < A.length; i++) {
            // Do something with A[i] here, such as...
            System.out.println(A[i]);
        }
        break;
    case DOWN:
        for (int i = A.length - 1; i >= 0; i--) {
            // Do something with A[i] here, such as...
            System.out.println(A[i]);
        }
        break;
    default:
        // Do something with A[t3], such as...
        System.out.println(A[t3]);
    }
}

FIXED 是唯一需要索引的选项,因此被编码为索引。 UPDOWN 不是索引,因此它们使用负数进行编码。用法类似于

public static void main(String[] args) {
    int[][][] m = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
    loop1(m, DOWN, 1, UP);
}

我的 System.out.println 示例,输出将是

7
8
3
4

The best I can think of at the moment is this:

static final int UP = -2, DOWN = -1;

static void loop1(int[][][] A, int t1, int t2, int t3) {
    switch (t1) {
    case UP:
        for (int i = 0; i < A.length; i++)
            loop2(A[i], t2, t3);
        break;
    case DOWN:
        for (int i = A.length - 1; i >= 0; i--)
            loop2(A[i], t2, t3);
        break;
    default:
        loop2(A[t1], t2, t3);
    }
}

static void loop2(int[][] A, int t2, int t3) {
    switch (t2) {
    case UP:
        for (int i = 0; i < A.length; i++)
            loop3(A[i], t3);
        break;
    case DOWN:
        for (int i = A.length - 1; i >= 0; i--)
            loop3(A[i], t3);
        break;
    default:
        loop3(A[t2], t3);
    }
}

static void loop3(int[] A, int t3) {
    switch (t3) {
    case UP:
        for (int i = 0; i < A.length; i++) {
            // Do something with A[i] here, such as...
            System.out.println(A[i]);
        }
        break;
    case DOWN:
        for (int i = A.length - 1; i >= 0; i--) {
            // Do something with A[i] here, such as...
            System.out.println(A[i]);
        }
        break;
    default:
        // Do something with A[t3], such as...
        System.out.println(A[t3]);
    }
}

FIXED is the only option which requires an index, and is thus encoded as an index. UP and DOWN are not indices, so they are encoded using negative numbers. Usage would go something like

public static void main(String[] args) {
    int[][][] m = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
    loop1(m, DOWN, 1, UP);
}

With my System.out.println example, the output would be

7
8
3
4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文