Java索引超出范围的错误矩阵乘法

时间:2015-03-22 19:12:06

标签: java multithreading 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();
    }
}

1 个答案:

答案 0 :(得分:1)

这对我有用:

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();