Java 索引越界错误矩阵乘法

Java Index out of bound error matrix multiplication

我收到一个索引超出范围的错误。

我在 netbeans 上设置了项目的参数并且工作正常。

但是我如何在代码中设置 n 的参数而不继续项目属性并更改那里的值?

当我尝试输入 static int N = 4 ; 时,我在整个代码中遇到错误,有人可以帮助我吗?

package matrix;

// performing matrix multiplication parallely by using two threads
// In thread 1 we will multiply matrix a and b and store in C with range of 0 to N/2
// In thread 2 we will multiply matrix a and b and store in C with range of N/2 to N
// For our convenience I'm used 4 instead of N ( we can replace 4 with N)

public class Mymainclass implements Runnable {
    static int n;

    // a and b are input matrix's 
    static int a[][];
    static int b[][];
    /* we will multiply the elements in a and b matrix's 
    * parallely by using two threads
    and will store in the c matrix sequentially.*/
    static int c[][];

    public Mymainclass(int n1) {
        n = n1;
        a = new int[n][n];
        b = new int[n][n];
        c = new int[n][n];
    }

    public void run() {
        int i, j, k;

        System.out.println("in thread1 class");

        for (i = 0; i < this.n; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < n; k++) {
                    this.c[i][j] += a[i][k] * b[k][j];
                }
            }
        }

        System.out.print("\n");
        System.out.println("Matrix c in thread1");
        for (int a = 0; a < n; a++) {
            for (int b = 0; b < n; b++) {
                System.out.print(c[a][b] + " ");
            }
            System.out.print("\n");
        }
    }

    public static void main(String[] args) {
        String n1 = args[0];
        n = Integer.parseInt(n1);
        System.out.println(n);

        Mymainclass th1 = new Mymainclass(n);

        int z = 1;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = z++;
            }
            System.out.print("\n");
        }

        z = 1;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                b[i][j] = z++;
            }
            System.out.print("\n");
        }

        System.out.println("Matrix a");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.print("\n");
        }

        System.out.print("\n");
        System.out.println("Matrix b");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(b[i][j] + " ");
            }
            System.out.print("\n");
        }

        System.out.print("\n");

        Thread t = new Thread(th1);
        t.start();
    }
}

这对我有用:

public class Mymainclass implements Runnable {
    static int n;

    // a and b are input matrix's
    static int a[][];
    static int b[][];
    /* we will multiply the elements in a and b matrix's
    * parallely by using two threads
    and will store in the c matrix sequentially.*/
    static int c[][];

    public Mymainclass(int n1) {
        n = n1;
        a = new int[n][n];
        b = new int[n][n];
        c = new int[n][n];
    }

    public void run() {
        int i, j, k;

        System.out.println("in thread1 class");

        for (i = 0; i < this.n; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < n; k++) {
                    this.c[i][j] += a[i][k] * b[k][j];
                }
            }
        }

        System.out.print("\n");
        System.out.println("Matrix c in thread1");
        for (int a = 0; a < n; a++) {
            for (int b = 0; b < n; b++) {
                System.out.print(c[a][b] + " ");
            }
            System.out.print("\n");
        }
    }

    static int N = 4 ;
    public static void main(String[] args) {

        Mymainclass th1 = new Mymainclass(N);

        int z = 1;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = z++;
            }
            System.out.print("\n");
        }

        z = 1;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                b[i][j] = z++;
            }
            System.out.print("\n");

        }


        System.out.println("Matrix a");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.print("\n");
        }

        System.out.print("\n");
        System.out.println("Matrix b");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(b[i][j] + " ");
            }
            System.out.print("\n");

        }

        System.out.print("\n");


        Thread t = new Thread(th1);
        t.start();
    }

}

要测量时间,只需将开始线程的代码更改为:

    ExecutorService service = Executors.newSingleThreadExecutor();
    long start = System.currentTimeMillis();
    Future<?> future = service.submit(th1);
    try {
        future.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    System.out.println("time: " + (System.currentTimeMillis() - start));
    service.shutdown();