push(int[][]) in a stack is duplicating the same value Java

MBH

I have a 2 dimensional array and stack:

int[][] data = new int[][];
Stack<int[][]> undoList = new Stack<int[][]>(10);

I wanna add the data after every change to keep it in undo list:

undoList.push(data);
// some changes to data array
undoList.push(data);
// some changes to data array
undoList.push(data);

then why do all the stack elements have the same value?

MBH

I solved it by copying it with this code as @Marco13 has mentioned in the comment above:

Deep Copy

public static int[][] deepCopyArray(int[][] oldArray){
        int[][] newArray = new int[oldArray.length][];
        for(int i=0; i<oldArray.length; i++)
        {
            newArray[i] = new int[oldArray[i].length];
            for(int j=0; j<oldArray[i].length; j++)
                newArray[i][j] = oldArray[i][j];
        }
        return newArray;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java: HashSet duplicating last value

From Dev

Pop consecutive items on a stack if value is the same

From Dev

Duplicating name characters in java?

From Dev

Duplicating column to same table in PostgreSQL

From Dev

Will a Java thread ID stay the same for the full stack?

From Dev

Jtable values duplicating the last value

From Dev

read last value from stack (java)

From Dev

Prevent grunt from duplicating files with the same name

From Dev

Duplicating text before a pattern to the same line?

From Dev

Prevent grunt from duplicating files with the same name

From Dev

Duplicating the value using echo PHP MySQL

From Dev

Stack=4 in Java bytecode. How does the Java Compiler compute the 4 value? (depth of the the stack)

From Dev

how to avoid duplicating JTable columns in java?

From Dev

Writing to File duplicating data the second time JAVA

From Dev

Java bytecode - efficient manner to replace a deep stack value

From Dev

Java bytecode - efficient manner to replace a deep stack value

From Dev

SQL LEFT JOIN without duplicating same row values

From Dev

angularjs duplicating controllers/displaying same data in multiple spots

From Dev

SQL LEFT JOIN without duplicating same row values

From Dev

How to add items to treeview without duplicating it to other node with the same name

From Dev

How can I avoid duplicating the same object on the screen using ID?

From Dev

Using the same values with different classes in different @media without duplicating

From Java

java stream How to merge same value to list

From Dev

Get same left shift value in Java as Python

From Dev

java stream How to merge same value to list

From Dev

method object return same value java

From Dev

Java QueryParam with key and value in the same parameter

From Dev

Getting same value of a member variable in java

From Dev

Option Set Text value duplicating....is there a property that I can check to avoid duplicating?

Related Related

  1. 1

    Java: HashSet duplicating last value

  2. 2

    Pop consecutive items on a stack if value is the same

  3. 3

    Duplicating name characters in java?

  4. 4

    Duplicating column to same table in PostgreSQL

  5. 5

    Will a Java thread ID stay the same for the full stack?

  6. 6

    Jtable values duplicating the last value

  7. 7

    read last value from stack (java)

  8. 8

    Prevent grunt from duplicating files with the same name

  9. 9

    Duplicating text before a pattern to the same line?

  10. 10

    Prevent grunt from duplicating files with the same name

  11. 11

    Duplicating the value using echo PHP MySQL

  12. 12

    Stack=4 in Java bytecode. How does the Java Compiler compute the 4 value? (depth of the the stack)

  13. 13

    how to avoid duplicating JTable columns in java?

  14. 14

    Writing to File duplicating data the second time JAVA

  15. 15

    Java bytecode - efficient manner to replace a deep stack value

  16. 16

    Java bytecode - efficient manner to replace a deep stack value

  17. 17

    SQL LEFT JOIN without duplicating same row values

  18. 18

    angularjs duplicating controllers/displaying same data in multiple spots

  19. 19

    SQL LEFT JOIN without duplicating same row values

  20. 20

    How to add items to treeview without duplicating it to other node with the same name

  21. 21

    How can I avoid duplicating the same object on the screen using ID?

  22. 22

    Using the same values with different classes in different @media without duplicating

  23. 23

    java stream How to merge same value to list

  24. 24

    Get same left shift value in Java as Python

  25. 25

    java stream How to merge same value to list

  26. 26

    method object return same value java

  27. 27

    Java QueryParam with key and value in the same parameter

  28. 28

    Getting same value of a member variable in java

  29. 29

    Option Set Text value duplicating....is there a property that I can check to avoid duplicating?

HotTag

Archive