Reputation: 684
So I have a matrix M1 that I build at the beginning of my method each method call (this is all done in a non-static environment). The issue is that, depending on an integer index, say n I want the matrix M2 (in method call #2 for example) to contain rows 1, 2... n of M1 (the number of rows that I need for M changes each method call, but the number of columns persist).
My method in pseudocode;
int myMethod(numRows, numColumns, n) {
Initialize M as matrix with dimensions(numRows, numColumns)
if (n > 0) {
**copy rows 1,2...,n from previous M matrix**
}
**do stuff with M**
return M[numRows][numColumns];
What is a smart way to accomplish this? I hope it's clear what I'm asking for. Something worth noting is that Mi can be 'taller' or 'shorter' (it's always exactly as 'wide') as Mj for i > j
Upvotes: 0
Views: 1841
Reputation: 36
Maybe return the array and pass the array as a parameter.
int[][] mymethod(int[][]oldArray, int noOfRows){
int[][] newArr;
if(noOfRows > 0){
//here copy from oldArray to new;
//sorry this is where I can't remember, you will have
//to do something to get column size maybe oldArray[0].length()
// but that may not work if each row has different length.
newArr = new int[noOfRows][];
for(int i=0; i < noOfRows; i++){
for(int j = 0; j < noOfRows; j++){
newArr[i][j] = oldArray[i][j];
}
}
}
return newArr;
}
after this just call the method. int [][] a = mymethod(dArray,5); int [][] b = mymethod(a, 2);
Upvotes: 1