something went wrong in my matrix program of java

something went wrong in my matrix program of java
java
Ethan Jackson

I made a matrix program in java which calculate the all similar matrix with the same determinant (there are a lot, I began for forty) . but I have a problem: In the class where I do the algorithm for to create the matrix 4x4 by another one of 2x2 in my class Nova I put one constructor of a matrix called "identidad" (4x4) and in a public void I set this with the input matrix "matriz" (4x4).After, in a public method I modify this matrix "identidad" for to make it a similar matrix of the "matriz" (with the help of another one of 2x2 called "matrix2"). I won't go into detail of the algorithm, but the case is that every time I call the class from the void main, the matrix "identidad" not set to "matriz", instead "identidad" goes with the last value. So, the similar matrix are every time more big.

Class Nova { private int [][] identidad = new int [10][10]; public Nova (int [][] matriz){ this.identidad = matriz; } public void setIdentidad(int [][] matriz) { this.identidad = matriz; } public int[][] getIdentidad(int[][] matrix2, int dimension, int d) { <code which modify the matrix "identidad" for to be similar to "matriz"> return identidad; } }

And now, you can see how I call this since the main class:

public class Main{ public static void main (String [] args){ <code for to create the matrix "matriz" (4x4)> Nova nova = new Nova(matriz); for (int os=0;os<40;os++){ nova.setIdentidad (matriz); matrix[os] = nova.getIdentidad(matrix2[os], dimension, determinant); } <output the result> } }

In the loop It must to create forty similar matrix, but smallest because the "identidad" in fact was reset to "matriz" with de order set. The reality is, it doesn't. Instead, every time get the matrix "identidad" create yet in the last time. And No way It's doing it. I did this with java 17. Even I try with java 21 without result. thanks.

Answer

It sounds like you've run into a classic Java puzzle. The problem isn't with your Java version or a bug, but with how Java handles objects like arrays. It's a very common thing to trip over.

The short answer is: you're not actually resetting the matrix. You're just pointing your identidad variable back to the same matrix in memory, which has already been changed.


The Problem: References vs. Copies

In Java, when you work with objects (and arrays are objects), variables don't hold the object itself. They hold a reference—think of it like a memory address or a signpost that points to where the actual object is stored.

When you do this in your constructor or your setIdentidad method:

this.identidad = matriz;

You are not copying the values from matriz into identidad. You are just telling identidad to point to the exact same array that matriz is pointing to.

Here's what happens in your loop:

  1. Iteration 1: You call getIdentidad(). This method modifies the array that identidad points to. Since matriz in your main method points to that very same array, it gets modified too! 😱

  2. Iteration 2: You call setIdentidad(matriz). You think you're resetting it, but you're just pointing identidad to the matriz... which is the one that got changed in the last step. So, nothing gets reset. You just start modifying the already modified matrix again.


The Solution: Make a Deep Copy

To fix this, you need to create a true copy of the array's values. You have to manually copy each element from the source matrix to the destination one.

Update your setIdentidad method and your constructor to actually copy the data, like this:

Java

class Nova { // It's better to initialize this in the constructor private int[][] identidad; // The constructor should also make a copy public Nova(int[][] matriz) { // Create a new array and copy the contents this.identidad = new int[matriz.length][]; for (int i = 0; i < matriz.length; i++) { this.identidad[i] = new int[matriz[i].length]; for (int j = 0; j < matriz[i].length; j++) { this.identidad[i][j] = matriz[i][j]; } } } // Your set method should also do a full copy public void setIdentidad(int[][] matriz) { // No need to check dimensions if they are always the same, // but this loop ensures a true copy. for (int i = 0; i < matriz.length; i++) { for (int j = 0; j < matriz[i].length; j++) { this.identidad[i][j] = matriz[i][j]; } } } public int[][] getIdentidad(int[][] matrix2, int dimension, int d) { // <your code which modifies "identidad"> return this.identidad; } }

Now, every time you call setIdentidad(matriz), it will properly overwrite identidad with the original, clean values from matriz, and your algorithm will start fresh for each of the 40 matrices. 👍


A Cleaner Alternative

An even better approach is often to create a new Nova object inside your loop. This keeps things cleaner because each calculation is completely independent, and you don't have to worry about resetting state.

Your main method would look like this:

Java

public class Main { public static void main (String [] args) { // <code to create the original "matriz"> // This is where you store the 40 results int[][][] matrix = new int[40][][]; for (int os = 0; os < 40; os++) { // Create a fresh Nova object for each calculation Nova nova = new Nova(matriz); // Now you don't even need the setIdentidad() call matrix[os] = nova.getIdentidad(matrix2[os], dimension, determinant); } // <output the result> } }

If you use this alternative, just make sure your Nova constructor does the deep copy we talked about above. Otherwise, you'll still end up modifying the original matriz from main!

Hope it helps!!

Related Articles