Help ArrayIndexOutOfBoundsException


Joined
Jul 10, 2008
Messages
1
Reaction score
0
I am making a set of classes to implement the Gauss-Seidel method to solve the linear equation Ax = b.

The Test.java creates a random matrix A, a random vector b, and uses the GaussSeidel instance to solve for the solution vector x. However, I keep getting an ArrayIndexOutOfBoundsException while creating the random matrix.

Test.java has a main method which includes the line:
VectorMatrix<Double> A = RandomGenerator.randomDiagonalMatrix(10, 256.0);

RandomGenerator.java looks sth like:
public class RandomGenerator {

public static VectorMatrix<Double> randomDiagonalMatrix(int N, double upperbound){
//generates a random, double, N by N diagonal matrix
VectorMatrix<Double> diagMatrix = new VectorMatrix<Double>(N, N);
Random generator = new Random();
for (int i = 0; i < N; i++){
diagMatrix.set(i, i, generator.nextDouble() * upperbound);
}
return diagMatrix;
}
}

and finally the VectorMatrix looks like:

public class VectorMatrix<T> implements Matrix<T> {
int N;
int M;
Vector<T> A;

public VectorMatrix(int N, int M) {
this.N = N;
this.M = M;
A = new Vector<T>(N*M);
}
public void set(int i, int j, T x) {
A.set(i + N*j, x);
}
public int size(){
return N*M;
}

}

I can't find what is the problem. Help!
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top