1

I have created one method called rowSwitching to switch data on two specific row in array. However, when i used it the array value at the main also changed even though i don't want to store it.

Here's my code ;

public class MatrixEx {//implements Matrix {

public static void print(int[][] input1, int[][] input2) {
    System.out.println("------------------ ------------------");
    for (int row = 0; row < input1.length; row++) {
        for (int col = 0; col < input1.length; col++) {
            System.out.format("%4d", input1[row][col]);
        }
        System.out.print("          ");
        for (int col = 0; col < input2.length; col++) {
            System.out.format("%4d", input2[row][col]);
        }
        System.out.println();
    }
    System.out.println("------------------ ------------------");
}

public static void main(String[] args) {

    int[][] input1 = {
        {7, 2, 1},
        {0, 3, -1},
        {-3, 4, -2}
    };

    int[][] input2 = {
        {-2, 8, -5},
        {3, -11, 7},
        {9, -34, 21}
    };

    System.out.format("%12s%4s%12s\n", "input1", " ", "input2");
    print(input1, input2);

    MatrixEx matrixFunc = new MatrixEx();
    print(input1, matrixFunc.rowSwitching(input1, 1, 2));

}

public int[][] rowSwitching(int[][] matrix, int row1, int row2) {
    System.out.format("%12s%4s%12s\n", "Before", " ", "After");
    int[] temp1 = new int[matrix.length];
    int[] temp2 = new int[matrix.length];
    int[][] result = matrix;

    row1 -= 1;
    row2 -= 1;

    for (int i = 0; i < temp1.length; i++) {
        temp1[i] = result[row1][i];
    }

    for (int i = 0; i < temp2.length; i++) {
        temp2[i] = result[row2][i];
    }

    for (int i = 0; i < result.length; i++) {
        result[row1][i] = temp2[i];
    }

    for (int i = 0; i < result.length; i++) {
        result[row2][i] = temp1[i];
    }        
    return result;
}

I want to keep the array value at the main, but for some reason it keeps changing after i use the method.

Naufal FR
  • 25
  • 4

1 Answers1

1

In Java, any Object type (including arrays) is passed by reference. This means that if you have a function

private void doThings(int[] items) {
  items[0] = 10;
}

And you call it:

int[] myList = new int[3] {1,2,3};
doThings(myList);
System.out.println(myList[0]); // Prints '10'

This is because doThings receives the reference to where the list is stored in memory, and alters the actual memory where it is stored - changing it in main as well.

You may want to re-think your program or copy the array

Community
  • 1
  • 1
Will Richardson
  • 7,780
  • 7
  • 42
  • 56
  • Can you give me some example for copying the array in this program ? I tried it but still doesn't work (the value still change). I really want to change the program, but unfortunately we must the use interface thus limiting what we can do. – Naufal FR Sep 22 '15 at 23:54
  • I think it will work if you change `int[][] result = matrix;` to `int[][] result = matrix.clone();` – Will Richardson Sep 23 '15 at 03:23
  • Hmm, i tried it but it still doesn't work. Somehow the value references still passed to the method :/ – Naufal FR Sep 23 '15 at 08:07
  • I don't know what the output you're expecting from your program is, so I can't really help further. – Will Richardson Sep 23 '15 at 08:11
  • So for example, i have 3x3 array with the value of : {(1,2,3),(4,5,6),(7,8,9)}. After we use the rowSwitching method, it will display the original number (before the array entered the method) and the modified array. So it will be like : {(1,2,3),(4,5,6),(7,8,9)} | {(7,8,9),(4,5,6),(1,2,3)} <- *First and third row changed* – Naufal FR Sep 24 '15 at 00:35
  • Welp, found the answer when browsing array multiplication. Looks like i need to change " int [][] result = input.clone " into " int [][] result = new int [3][3] " in the rowSwitching method. Anyway, thanks for the response. – Naufal FR Sep 24 '15 at 01:48