0

I try to transpose matrix by reference, but without success, the matrix in the main stays in the same state. what can I do?

p.s. the function must to be "void"

thank you!

 public static void trans(int a[][]) {
        int n = a.length;
        int m = a[0].length;
        int b[][] = new int[m][n];
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[0].length; j++) {
                b[i][j] = a[j][i];
            }
        }       
        a = b;
}
    public static void main(String[] args) {
        int a[][] = {{1,2,3},
                     {4,5,6}};  
        trans(a);

        System.out.println("*****************************");
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[0].length; j++) {
                System.out.print(a[i][j]+"\t");
            }
            System.out.println();
        }
    }
ZoharYosef
  • 247
  • 1
  • 2
  • 11

2 Answers2

1

The main mistake is the line

a = b;

Here you assign link to array b to the link to array a (this link is a local variable).

Either you should copy array instead of assigning link or you should use initial array a inside the loop. Of course, all this is possible if the matrix is quadratic (nxn).

Copying an array (ineffective way!) would look like:

for (int i = 0; i < dim; ++i)
    System.arraycopy(b[i], 0, a[i], 0, dim);

Processing inside array would be like this:

int dim = a.length;
assert a[0].length == dim; // quadratic

for (int i = 0; i < a.length; ++i)
    for (int j = 0; j < i; ++j)
         a[i][j] = a[j][i];

Also, nowadays the immutable behavior is preferred over mutable arrays, so you may make your function return the new array instead of the mutating the old one (the solution you may find inside @AlexFitzpatrick's answer).

As the last thought, you may pass the reference to array to the method. This can be achieved with some wrapper or with built-in AtomicReference.

Then your method will accept not int[][], but AtomicReference<int[][]>, so it will finally look like:

public static void trans(AtomicReference<int[][]> aWrapper) {
...
    int[][] a = aWrapper.get();
    for ...
        for ...
            b[i][j] = ...
    aWrapper.set(b);
}
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
0

Your last line in the trans function does not replace the value of the original array, it simply reassigns the pointer of the parameter in that function.

The easiest solution is to return the new array.

public static int[][] trans(int a[][]) {
    int n = a.length;
    int m = a[0].length;
    int b[][] = new int[m][n];
    for (int i = 0; i < b.length; i++) {
        for (int j = 0; j < b[0].length; j++) {
            b[i][j] = a[j][i];
        }
    }       
    return b;
}
public static void main(String[] args) {
    int a[][] = {{1,2,3},
                 {4,5,6}};

    int b[][] = trans(a);

    System.out.println("*****************************");
    for (int i = 0; i < b.length; i++) {
        for (int j = 0; j < b[0].length; j++) {
            System.out.print(b[i][j]+"\t");
        }
        System.out.println();
    }
}
Alex Fitzpatrick
  • 643
  • 5
  • 10
  • thanks, I knew that, but I need to do transpose with a "void" function... – ZoharYosef Mar 19 '15 at 19:24
  • Then you need to pass a reference to the array to replace. Note if there is "requirement" for a void function then this is likely a homework question, so you should work harder. – Alex Fitzpatrick Mar 19 '15 at 19:26