Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# drawing a matrix in Java -HELP!!!

Naf Rash
Ranch Hand
Posts: 85
I need to draw a similarity matrix that holds a number of values. How do i do this? An example is provided below. The places where values need to be added are indicated by an x. I could specify which array elements must be empty ie, = to null but the matrix can change shape and thus needs to consistently enforce the below:

1 2 3 4
1 0
2 x 0
3 x x 0
4 x x x 0

Layne Lund
Ranch Hand
Posts: 3061
What have you done so far to find the solution yourself? What problems have you encountered? Please provide some code that you have tried as well as compiler errors and output, if any.
Layne

Naf Rash
Ranch Hand
Posts: 85
At the moment I am trying to do the matrix in an array. You see I am not very good at Java and don't knoiw much about. Maybe I shouldn't be using an array- if that is the case could you suggest some alternative structures please.
At the moment this is what I have come up with:

int row1= rsmd.getRowCount(), row2= rsmd.getRowCount();
int[][] distanceMatrix;
distanceMatrix= new int [row1][row2];
/*This produces a matrix with the same number of column as rows. The size of the matrix changes since it stores some calculations that have been carried out on database rows. As a result the number of rows that the database has is called for and this determines the size of the matrix.*/
My problems begin after this. I could say that if the row number is equal to the column number then place a 0 in this element.ie, for a 2*2 matrix if row number is 1 and column number is 1 then put a 0 there. (I don't know how to code this).
I could also say that if the column number(for the column that we are considering) is greater than the row number(for the row we are considering )then leave the element blank. Again I don't know how I would code this.
I could then enter the values into the remaining array elements. However, this is also difficult because all the calculation values are placed into an array (which then need to be put into the matrix). The fact that the size of the matrix varies depending on the database makes this difficult.