0

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

Nyfiken Gul
  • 654
  • 4
  • 20
  • 1
    you can also make use of a general method and use -deep-copy technology http://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java or http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object-in-java – lordkain Jan 31 '17 at 13:54
  • Right, thank you! How would you go about keeping the old M between runs? Keep it as an attribute in my class? – Nyfiken Gul Jan 31 '17 at 13:58

2 Answers2

1

You could use a two dimensional vector, and then perhaps the clone() method. The awesome thing about vector is that it can grow and shrink as you need it to.

Community
  • 1
  • 1
goliath16
  • 101
  • 1
  • 1
  • 6
  • Thank you! Feels like it should be able to be done with arrays though (the rest of my program uses arrays so) – Nyfiken Gul Jan 31 '17 at 14:27
  • No worries! It does look like [arrays](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html) have a .copyOf(T[] original_arr, int newlength) -Disclaimer, I've never used the method, but it looks pretty handy. – goliath16 Feb 01 '17 at 13:59
1

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);

manuel
  • 36
  • 1
  • Thanks for the answer, that's what I got since I asked aswell - although it feels like there should be some more time-efficient way to do this maybe it has to do! Thanks for your answer :) – Nyfiken Gul Jan 31 '17 at 14:47
  • 1
    No problem. I am also looking for more efficient ways of doing things. But first have it done in the most easy way you can have it done then refactor it with a more elegant and efficient way. I will keep looking back to this post to see if we can get a better solution. Anyways, cheers! – manuel Jan 31 '17 at 14:50
  • 1
    @NyfikenGul you may want to see http://stackoverflow.com/questions/5617016/how-do-i-copy-a-2-dimensional-array-in-java?rq=1. it has something very interesting regarding using System.Arraycopy. that way you only need to iterate the rows and then use that method. I don't know if that more efficient but it is less code. – manuel Jan 31 '17 at 15:03
  • Thank you, I'll look into that and let you know how it goes! – Nyfiken Gul Jan 31 '17 at 15:15
  • Actually it was even worse time-wise, iterating over the rows and columns actually worked quicker. Strange. – Nyfiken Gul Jan 31 '17 at 15:21