变量未初始化 java
Variable not initialising java
第 38 行 -
pool.submit(new Multi(N,i,j,a,b,c));
我有一个未初始化的变量 j
。我不确定所有其他人都已经初始化出了什么问题,这在过去的一个小时里一直困扰着我。
有人可以帮我解决这个错误吗?
public class Matrix1
{
public final static int N = 4; //Random size of matrix
public static void main(String[] args) throws InterruptedException
{
long startTime = System.currentTimeMillis();
//Create and multiply matrix of random size N.
double [][] a = new double [N][N];
double [][] b = new double [N][N];
double [][] c = new double [N][N];
int i,j,k;
for(i = 0; i < N ; i++)
for(j = 0; j < N ; j++)
{
a[i][j] = i + j;
b[i][j] = i * j;
}
ExecutorService pool = Executors.newFixedThreadPool(1);
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
{
c[i][j] = 0;
for(k = 0; k < N; k++)
c[i][j] += a[i][k] * b[k][j]; //C matrix calculated from a and B matrices.
}
pool.submit(new Multi(N,i,j,a,b,c));
pool.shutdown();
pool.awaitTermination(1, TimeUnit.DAYS);
long endTime = System.currentTimeMillis();
System.out.println("Calculation completed in " +
(endTime - startTime) + " milliseconds");
}
static class Multi implements Runnable
{
final int N;
final double [][] a;
final double [][] b;
final double [][] c;
final int i;
final int j;
public Multi(int N, int i, int j, double[][] a, double[][] b, double[][] c)
{
this.N=N;
this.i=i;
this.j=j;
this.a=a;
this.b=b;
this.c=c;
}
@Override
public void run()
{
for(int k = 0; k < N; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
}
您试图在其循环之外访问虚拟变量 j
。试试下面的代码:
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
c[i][j] = 0;
for (k = 0; k < N; k++) {
c[i][j] += a[i][k] * b[k][j]; // C matrix calculated from a and B matrices.
}
pool.submit(new Multi(N,i,j,a,b,c));
pool.shutdown();
pool.awaitTermination(1, TimeUnit.DAYS);
}
}
第 38 行 -
pool.submit(new Multi(N,i,j,a,b,c));
我有一个未初始化的变量 j
。我不确定所有其他人都已经初始化出了什么问题,这在过去的一个小时里一直困扰着我。
有人可以帮我解决这个错误吗?
public class Matrix1
{
public final static int N = 4; //Random size of matrix
public static void main(String[] args) throws InterruptedException
{
long startTime = System.currentTimeMillis();
//Create and multiply matrix of random size N.
double [][] a = new double [N][N];
double [][] b = new double [N][N];
double [][] c = new double [N][N];
int i,j,k;
for(i = 0; i < N ; i++)
for(j = 0; j < N ; j++)
{
a[i][j] = i + j;
b[i][j] = i * j;
}
ExecutorService pool = Executors.newFixedThreadPool(1);
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
{
c[i][j] = 0;
for(k = 0; k < N; k++)
c[i][j] += a[i][k] * b[k][j]; //C matrix calculated from a and B matrices.
}
pool.submit(new Multi(N,i,j,a,b,c));
pool.shutdown();
pool.awaitTermination(1, TimeUnit.DAYS);
long endTime = System.currentTimeMillis();
System.out.println("Calculation completed in " +
(endTime - startTime) + " milliseconds");
}
static class Multi implements Runnable
{
final int N;
final double [][] a;
final double [][] b;
final double [][] c;
final int i;
final int j;
public Multi(int N, int i, int j, double[][] a, double[][] b, double[][] c)
{
this.N=N;
this.i=i;
this.j=j;
this.a=a;
this.b=b;
this.c=c;
}
@Override
public void run()
{
for(int k = 0; k < N; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
}
您试图在其循环之外访问虚拟变量 j
。试试下面的代码:
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
c[i][j] = 0;
for (k = 0; k < N; k++) {
c[i][j] += a[i][k] * b[k][j]; // C matrix calculated from a and B matrices.
}
pool.submit(new Multi(N,i,j,a,b,c));
pool.shutdown();
pool.awaitTermination(1, TimeUnit.DAYS);
}
}