This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

Hi, I am having a problem with trying to make a two-dimensional array available to another class. I am trying to do this by placing the array into a separate method from which the array can be obtained. The values that the array holds are of type double but if when i am declaring the method I define it as returning a double there is a problem because the elements ie, element 0 is known as type integer- i have tried changing it so that it is double but the size or element being referred to in an array is of type int. How can I amend the current piece of code so that there are no errors? I am also going to include the method where the array (distanceMatrix) is created. The bit I am having problems with is the last bit; passing Matrix where I am currently getting the error 'This method must return of type double'. Please put forward any suggestions that you may have.

ublic void manhattan() { theSum =new int[theCount]; sum = new int [theRowCount][theCount];

/* Comparing each record attribute with the next record. The sum stores the value of the * differences between records (based on single attributes). The first for stmt looks at * one row at a time- the second for stmt the compares this row to all the other rows. * For every row comparison all the attributes are considered. NOTE: the second for stmt * starts at 2 so that the second row is considered (the first row is dealt with in the * first for stmt).*/ for (int i=1; i<theRowCount; ++i){ for (int all = 2; all<theRowCount; ++all){ for (int k=1; k<theCount; ++k)//attributes

/* Carry out the calculations to work out the manhattan distance. If the attribute value being * considered of type int then an exception occurrs and is caught in the catch clause- if the 2 * values differ then the distace is equal to 1 else it is equal to 0.*/ try{ for (int c=1; c<theCount; ++c){ theSum[c]=(sum[i][k] - sum[all][k]); }

/*Add all the attribute differences together and then square them. Stores the resulting values * in an array. */ for (int ts=0; ts<theSum.length-1; ++ts){ distance = distance + theSum [ts]; } //check to see if the result of the calculation is negative. If so, make it positive. if (distance<0){ distance = distance*distance; } } catch (NumberFormatException nfe){ if (sum[i][k] == sum[all][k]){ distance = 0; } else if (sum[i][k] == sum[all][k]){ distance =1; } }//end catch

/*Devise the matrix. For the tuples being considered ther manhattan distance is entered in the * corresponding element of the array.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.*/ int row1= theRowCount, row2= theCount; double [][] distanceMatrix; distanceMatrix= new double [row1][row2]; for (i=1;i<row1; ++i){ for (all = 1; all<row2; ++row2) distanceMatrix[i][all] =distance; //return distanceMatrix[i][all]; } } }

}

public double passingMatrix(){ int i=0; int j=0; for ( double ij=0; ij<distanceMatrix.length; ij++) return distanceMatrix[i][j]; }

I'm not sure I completely understand your question, but there are a couple of problems I see with this code that might help you:

1. Since you are returning a 2-dimensional array (distanceMatrix[i][j]), the return type must match, like this: 2. I don't think you really want the return statement inside of that for loop. The first time the loop executes, the method will return.

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"

<hr></blockquote> Hi Naf, you probably need to keep the int's as int's, because the double will not count like you want it to. You have to typecast the return statement at the end. return distanceMatrix[(double)i][(double)j]; This way it will return two double objects instead of int's. Also change your for loop to int ij as well, because the distanceMeatrix.length is measured in int's. HTH, and I hope that it is correct. You may have to play around with the type cast, but I believe it is the right path. It would be great if somebody else could chime in on this one. Thanks Gabe

2. I don't think you really want the retur[n statement inside of that for loop. The first time the loop executes, the method will return

definately [ March 03, 2004: Message edited by: Gabriel White ]

This method has a few problems. First, it returns a double according to its prototype. It seems you want to return data of type double[][]. So make that change. Second, you have a return in a for loop, which means the first execution of that loop is going to cause a return and the rest of the loop will never execute. Third, there's no variable called distanceMatrix in the scope of this method, so the compiler's not going to know what variable you're trying to access. Create a new one. Now let's fix these problems and look at the underlying logic of the loop itself:

Note the XXX's above. Since you have to create a new matrix in this method, you have to know how big it's going to be and you don't have that information here in the scope of this method. So, whoever calls this method is going to have to tell you how big that distance matrix is supposed to be. Also, what is the purpose of this method? It's no longer to "pass" a matrix...of course it is passing a matrix back, but all non-void methods pass data back. So that doesn't really tell us specifically what this method is doing. What is this method doing? It's creating a new distance matrix and initializing it to a certain set of values. So let's retitle our method and include the arguments as below:

Now we're getting closer. But does this method really do what we want it to do? It seems to me that you're trying to initilize every value of the matrix to some value that corresponds to its position from the upper left cell. Is that what your for loop is doing? Let's say I call getNewDistanceMatrix(3, 2)...what happens in that for loop? 1. On the first iteration of the for loop, i, j, and ij are all 0. On subsequent iterations, only ij will increment by 1 each time. In the first iteration, [i][j] is [0][0] and is set to ij, which is 0. 2. On the next iteration, i and j are still 0, ij is 1. So, [0][0] gets reset to 1. ...etc, each iteration increases the upper left cell of the matrix by one each time. So, the loop we have above is exactly the same as:

Is this really the functionality we want? I don't think so. We want something like (for a 5x5 matrix, let's say): 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 If this is the functionality we're going after, then the for loop should be: