Java 在循环中选择变量

标签 java variables loops

我想知道是否有更有效的方法来在循环中的变量之间进行选择。我所拥有的以下代码可以工作,但如果可能的话,我希望有更好的方法。

Map<Character, Character> axes = new HashMap<Character, Character>();

(...)

for (int w = 0; w < image.getWidth(); w++) {
    for (int h = 0; h < image.getHeight(); h++) {
        for (int d = 0; d < depth; d++) {
            int x = axes.get('x') == 'w' ? w : (axes.get('x') == 'h' ? h : d);
            int y = axes.get('y') == 'w' ? w : (axes.get('y') == 'h' ? h : d);
            int z = axes.get('z') == 'w' ? w : (axes.get('z') == 'h' ? h : d);

            (...)

        }
    }
}

在上面的内容中,我需要将图像的某些坐标分配给具有深度的 3D 坐标,但它使用的边会根据其面向的方向而变化。有没有更快的方法来执行代码而不必进行单独的循环?

最佳答案

您可以尝试如下操作:

int[] coords = new int[3];
int width = image.getWidth();
int height = image.getHeight();
for (coords[0] = 0; coords[0] < width; ++coords[0])
{
    for (coords[1] = 0; coords[1] < height; ++coords[1])
    {
        for (coords[2] = 0; coords[2] < depth; ++coords[2])
        {
            int x = coords[x_idx];
            int y = coords[y_idx];
            int z = coords[z_idx];
            …
        }
    }
}

关于Java 在循环中选择变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21081753/

相关文章:

ruby-on-rails - 如何在中间件中设置一个可在我的所有应用程序中访问的变量?

Java:我如何对 Windows XP 中的计算机关机使用react?

java - 我正在尝试使用 if 语句构建一个基本计算器

javascript - JavaScript中变量的范围是什么?

JavaScript:将对象存储为固定值?

matlab - matlab中无循环向量中的数字求和

c - 为什么我的程序跳过 for 循环? C

Android - 每 5 秒循环部分代码

java - 包含 JSP 的 Spring Controller @RequestMapping

java - 如何在Android中返回FileInputStream的值