Solved

# Building Array....Compile error.

Posted on 2004-09-22

--------------------------------------------------------------------------------------------------

Code begin

--------------------------------------------------------------------------------------------------

/*

instance variables

-----------------------------------------

private double[][] M array representing the Matrix

private final int MAX_SIZE constant representing the maximum size of a Matrix;

it should be set to 100

-----------------------------------------

The Code:

public methods

-----------------------------------------

Matrix(int sizeN) A constructor that creates a Matrix of

size N x N filled with zeros

N must be in the range [0, MAX_SIZE].

If N is negative, create a Matrix of size 0 x 0.

If N is larger than MAX_SIZE, create a Matrix

of size MAX_SIZE x MAX_SIZE.

Matrix(int sizeN, double[][] initValues) A constructor that creates a Matrix of size N x N

and fills it with the values found in the array initValues.

N must be in the range [0, MAX_SIZE]. If N is negative,

create a Matrix of size 0 x 0. If N is larger than MAX_SIZE,

create a Matrix of size MAX_SIZE x MAX_SIZE.

-----------------------------------------

*/

public class Matrix

{

//initialize array M

private double[][] M;

//variable to set max size of array

private final int MAX_SIZE = 100;

//Constructor One

//Create a new Matrix, initializing it to zeros

//The size must be a value in the range [0, MAX_SIZE]

public Matrix(int sizeN)

{

if (sizeN > MAX_SIZE)

{

System.out.println("The maximum size of a matrix is " +

MAX_SIZE);

sizeN = MAX_SIZE;

}

else if (sizeN < 0)

{

System.out.println("The size of a matrix cannot be negative");

sizeN = 0;

}

//build array M with sizeN x sizeN initalized to 0

M = new double[sizeN][sizeN];

for (int i = 0; i < sizeN; i++)

for (int j = 0; j < sizeN; j++)

M[i][j] = 0.0;

}

//Constructor Two

//Create a new Matrix, initializing it with the above parameters

//The dimensions must be in the range [1, MAX_SIZE]

public Matrix(int sizeN, double[][] initValues)

{

initValues = new double[sizeN][sizeN];

double fillArray;

for (int i = 1; i < MAX_SIZE; i++)

fillArray = 0;

for (int j = 1; j < MAX_SIZE; j++)

fillArray += initValues [i][j];

}

--------------------------------------------------------------------------------------------------

code end

--------------------------------------------------------------------------------------------------

Problem: (Compile error)

--------------------------------------------------------------------------------------------------

cannot resolve symbol

symbol : variable i

location: class Matrix

fillArray += initValues [ i ] [ j ];

^

What's the problem? Up to "//Constructor Two" the code compiles without error.

As you probably know I'm writing a "matrix" class.

Last do you see any flaws with the rest of the build either in code itself or my approach?

Thanks.