java - 再次重新执行for循环

标签 java arrays multidimensional-array

我有一个具有以下值的二维数组

1,2,3
1,0,2
0,1,2 

和一个具有以下值的数组

0,1,2,3,4,5,6,7,8 

我想要实现的是首先从数组[0]开始

 matrix[0][0] * array[0] = result1
 matrix[0][1] * array[1] = result2
 matrix[0][2] * array[2] = result3
 matrix[1][0] * array[0] = result4
 matrix[1][1] * array[1] = result5
 matrix[1][2] * array[2] = result6
 matrix[2][0] * array[0] = result7
 matrix[2][1] * array[1] = result8
 matrix[2][2] * array[2] = result9    

之后它将返回重做循环,但这次从 array[3] 开始

 matrix[0][0] * array[3] = result10
 matrix[0][1] * array[4] = result11
 matrix[0][2] * array[5] = result12
 matrix[1][0] * array[3] = result13
 matrix[1][1] * array[4] = result14
 matrix[1][2] * array[5] = result15
 matrix[2][0] * array[3] = result16
 matrix[2][1] * array[4] = result17
 matrix[2][2] * array[5] = result18  

之后它将返回重做循环,但这次从数组[6]开始

 matrix[0][0] * array[6] = result19
 matrix[0][1] * array[7] = result20
 matrix[0][2] * array[8] = result21
 matrix[1][0] * array[6] = result22
 matrix[1][1] * array[7] = result23
 matrix[1][2] * array[8] = result24
 matrix[2][0] * array[6] = result25
 matrix[2][1] * array[7] = result26
 matrix[2][2] * array[8] = result27

我尝试了一下,但无法达到我想要的输出。请帮忙。

<小时/>
public class Test1 {
    public static void main (String[]args) {
        int matrix[][] = new int[][] {
                                    {1,2,3},
                                    {1,0,2},
                                    {0,1,2}
                                 };
        int array[] = { 0,1,2,3,4,5,6,7,8 };

        int count = 0;
        int a = 0;
        int row = 0, col = 0;
        boolean done = false;
        while(!done) {
            for (row = 0; row < matrix.length; row++) {
                for (col = 0; col < matrix.length; col++) {
                    a = matrix[row][col] * array[count];
                    if(col > 0) {
                        count++;
                    }
                    if(count > col && row != col) {
                            count = 0;
                    }
                    if(row == matrix.length-1 && col == matrix.length-1) {
                        count = count + 1;  
                    }   
                        System.out.println(count);
                }   
            }
            if(row == matrix.length-1 && col == matrix.length-1) {
                count = count + 1;  
            }   
            if(count == array.length) {
                done = true;
                break;
            }
        }
    }
}

最佳答案

这是您可以使用的代码:它输出每次乘法的结果与相应的结果索引。

public static void main(String[] args) {
    int matrix[][] = new int[][] { { 1, 2, 3 }, { 1, 0, 2 }, { 0, 1, 2 } };
    int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };

    int globalCount = 0;
    int count = 0;
    int a = 0;
    int row = 0, col = 0;
    boolean done = false;

    for (count = 0; count < array.length; count += 3) {
        for (row = 0; row < 3; row++) {
            for (col = 0; col < 3; col++) {
                a = matrix[row][col] * array[count++];
                System.out.println("Result" + globalCount++ + " " + a);

            }
            count -= 3;

        }

    }
}

输出:

Result0 0
Result1 2
Result2 6
Result3 0
Result4 0
Result5 4
Result6 0
Result7 1
Result8 4
Result9 3
Result10 8
Result11 15
Result12 3
Result13 0
Result14 10
Result15 0
Result16 4
Result17 10
Result18 6
Result19 14
Result20 24
Result21 6
Result22 0
Result23 16
Result24 0
Result25 7
Result26 16

关于java - 再次重新执行for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26427805/

相关文章:

java - 如何查找列表中与特定结构匹配的所有字符串?

java - 如何打印二维数组的结果

java - 由于运行时异常,MessageBrokerServlet 初始化失败

java - 安卓 : is it safe to create a View and a Handler in a worker thread

java - 转换 map 中的两个原始数组。并将结果映射转换为该数组

javascript - 我想匹配 Angular foreach 中的特定 id

java - 多维数组转置

C++多线程指针指向指针

numpy - 快速 ndarray bool 交集

java - 如何在java中的二维数组中找到元素后计算X次