Naf Rash

Ranch Hand

Posts: 85

posted 13 years ago

Is there any way of returning an array?

I have created an array that holds a series of numbers. However, i need to use this in another method which basically gets the matrix and returns it so that it can be retrieved by another class. Can this be done? I know that to an array can be passed to another method but my issue is with resepct to actually returning it so that it can be manipulated later on.

I am enclosing some code so that you may understand where my problems lay. The errors that I have so far are:

The method getMatrix() in the type distances is not applicable for the arguments (double[][])

Syntax error on keyword "double"; "interface", "class" expected - this is with respect to the actual method getMatrix.

public double[][] 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[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[k] == sum[all][k]){

distance = 0;

}

else if (sum[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[all] =distance;

getMatrix(distanceMatrix);/*The distance matrix is passed call-by-refernce*/

//return distanceMatrix;//[all];

}

}

}

}

public double getMatrix(theDistanceMatrix[][]) {

return theDistanceMatrix[j];

}

I have created an array that holds a series of numbers. However, i need to use this in another method which basically gets the matrix and returns it so that it can be retrieved by another class. Can this be done? I know that to an array can be passed to another method but my issue is with resepct to actually returning it so that it can be manipulated later on.

I am enclosing some code so that you may understand where my problems lay. The errors that I have so far are:

The method getMatrix() in the type distances is not applicable for the arguments (double[][])

Syntax error on keyword "double"; "interface", "class" expected - this is with respect to the actual method getMatrix.

public double[][] 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[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[k] == sum[all][k]){

distance = 0;

}

else if (sum[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[all] =distance;

getMatrix(distanceMatrix);/*The distance matrix is passed call-by-refernce*/

//return distanceMatrix;//[all];

}

}

}

}

public double getMatrix(theDistanceMatrix[][]) {

return theDistanceMatrix[j];

}

Ben Wood

Ranch Hand

Posts: 342

posted 13 years ago

Hi.

One potential problem I can see here...

there seems to be some difference between what you are supposed to be returning from this method, and what is actually being returned. I think all of the following modified versions should work, but you will have to chose which is the one you actually need!

One potential problem I can see here...

there seems to be some difference between what you are supposed to be returning from this method, and what is actually being returned. I think all of the following modified versions should work, but you will have to chose which is the one you actually need!

SCJP 1.4, www.gsi3d.org.uk

Joseph Kampf

Greenhorn

Posts: 26

posted 13 years ago

Here is a version that should compile. (I don't have one of the classes you are using.) But you will get some errors about loss of precision.

There were many errors in this. I suggest you use an IDE for syntax checking such as Eclipse. You were missing matching parens, brackets, semicolons, you mis capilized and misspelled some of your variable names.

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

public class Clusteringclass {

distances dist = new distances();

double theRow = 1;

double theCoulmn =0;

double newRow = 1;

public Clusteringclass(){

dist.getMatrix();

}

private void obtainClusters(double distanceMatrix[][])

{

for (int i=0;i<distanceMatrix.length; ++i){

for (int all = 1; all<distanceMatrix.length; ++all){

for (double checkDistanceVal = 100; (checkDistanceVal > distanceMatrix[i][all]) && (distanceMatrix[i][all] !=0)

{

checkDistanceVal = distanceMatrix[i][all];/*This will hold the final value ie, the value with the smallest distance.*/

double row = i;

double column = all;/*These assignments ensure that the corresponding row and column numbers are stored for the

shortest value.*/

}

}

}

}

public double[][] addElement(double[][] distanceMatrix, double row, double column ){

double[][] newArray = new double [distanceMatrix.length - 1][distanceMatrix.length - 1];/*Creates a brand new array of greater

length*/

for ( int i = 0; i < distanceMatrix.length; i++){

System.out.println (i+"\n");

if (i != row && i != column){/*As long as the element of distance matrix being covered is not*/

newArray[i - 1] = distanceMatrix[i];/*Assigns elements of the distance matrix to the new Array. use -1 because obviously

the array size will decrease since the rows that are of the shortest distance will be amalgamated into 1. */

}

}

for (int i = 0; i < distanceMatrix.length; ++i){

newArray[row][column] = Math.min(distanceMatrix [row][i], distanceMatrix [column][i]);

/*Puts the minimum value into the new array at the beginning.*/

++newRow;

/*Only increment the row value because going to input the new values downwards. */

}

distanceMatrix = newArray;

return distanceMatrix;

}

}

There were many errors in this. I suggest you use an IDE for syntax checking such as Eclipse. You were missing matching parens, brackets, semicolons, you mis capilized and misspelled some of your variable names.

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

public class Clusteringclass {

distances dist = new distances();

double theRow = 1;

double theCoulmn =0;

double newRow = 1;

public Clusteringclass(){

dist.getMatrix();

}

private void obtainClusters(double distanceMatrix[][])

{

for (int i=0;i<distanceMatrix.length; ++i){

for (int all = 1; all<distanceMatrix.length; ++all){

for (double checkDistanceVal = 100; (checkDistanceVal > distanceMatrix[i][all]) && (distanceMatrix[i][all] !=0)

{

checkDistanceVal = distanceMatrix[i][all];/*This will hold the final value ie, the value with the smallest distance.*/

double row = i;

double column = all;/*These assignments ensure that the corresponding row and column numbers are stored for the

shortest value.*/

}

}

}

}

public double[][] addElement(double[][] distanceMatrix, double row, double column ){

double[][] newArray = new double [distanceMatrix.length - 1][distanceMatrix.length - 1];/*Creates a brand new array of greater

length*/

for ( int i = 0; i < distanceMatrix.length; i++){

System.out.println (i+"\n");

if (i != row && i != column){/*As long as the element of distance matrix being covered is not*/

newArray[i - 1] = distanceMatrix[i];/*Assigns elements of the distance matrix to the new Array. use -1 because obviously

the array size will decrease since the rows that are of the shortest distance will be amalgamated into 1. */

}

}

for (int i = 0; i < distanceMatrix.length; ++i){

newArray[row][column] = Math.min(distanceMatrix [row][i], distanceMatrix [column][i]);

/*Puts the minimum value into the new array at the beginning.*/

++newRow;

/*Only increment the row value because going to input the new values downwards. */

}

distanceMatrix = newArray;

return distanceMatrix;

}

}