0
public static int[][] submatrix(int[][] arr, int i, int j)
{
    int[][] copyArr = arr;
    int[][] newArr = new int[copyArr.length - 1][copyArr[0].length - 1];
    
    for(int k = 0; k < copyArr[0].length; k++)
    {
        copyArr[j - 1][k] = 0;
    }
    
    for(int x = 0; x < copyArr.length; x++)
    {
        copyArr[x][i - 1] = 0;
    }
    printMatrix(copyArr);
    System.out.println();
    int row = 0;
    int col = 0;
    
    for(int x = 0; x < copyArr.length; x++)
    {
        for(int y = 0; y < copyArr[0].length; y++)
        {
            if(copyArr[x][y] != 0)
            {
                newArr[row][col] = copyArr[x][y];
                col += 1;
                if(col == newArr[0].length)
                {
                    col -= newArr[0].length;
                    row += 1;
                }
            }
        }
    }
    return newArr;
}

When I run this and use the 2d array:

int[][] matrixI = new int[][]{{11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34}, {41, 42, 43, 44}};

It correctly calculates the sub matrix, but when I call the method again on the same 2d array with different parameters(for the row and column I am getting rid of) it uses the array that was made when I called it the first time. I tried to use a copy of the array to avoid it changing the value of the inputted arr but it is still changing it.

The program is changing my value of arr when I dont want it to.

Kian Power
  • 11
  • 1
  • Don't you need nested for loops to adequately and deeply copy the 2D array? Or a single for loop and arraycopy calls? – Hovercraft Full Of Eels May 22 '23 at 02:21
  • 1
    Despite a copy being implied the line `int[][] copyArr = arr;` actually doesn't do a deep copy of `arr`. Both `copyArr` and `arr` are pointing to the same thing. So, any changes made to `copyArr` will change `arr`. This question is possibly a duplicate of: https://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java – Stephen Quan May 22 '23 at 02:25
  • Thank you, you were right about the copying confusion. I used a loop instead to iterate through and create a replica array and it works now. – Kian Power May 22 '23 at 02:30

2 Answers2

0

Assigning a variable to another variable in Java doesn't create a copy. It creates a second reference to the same object, or array in this case. You need to use a method to actually create a copy if that is your intention: https://www.baeldung.com/java-array-copy

aled
  • 21,330
  • 3
  • 27
  • 34
0

You won't be able to just assign copyArr to arr to make a legitimate copy.
Doing so will cause any updates within arr to also happen in copyArr.

How that is useful, I'm not sure.

You'll need to iterate over the values of arr and assign them to the corresponding indices of copyArr.

It's fairly simple, you can use the following code.

int[][] copyArr = new int[arr.length][];
for (int index = 0; index < arr.length; index++)
    System.arraycopy(arr[i], 0, copyArr[i], 0, arr.length);
Reilas
  • 3,297
  • 2
  • 4
  • 17