Deep copying a 2d array in Java

0 votes

Can someone guide me how to perform a deep copy of a boolean[][] array? I tried using .clone() on my 2d boolean array but it was a fail.

Any suggestions are welcomed.

Nov 12, 2018 in Java by 93.lynn
• 1,560 points
311 views

1 answer to this question.

0 votes

I guess, for deep copying you will have to iterate over your 2 dimensional array. You can refer the following code for the same:

public static boolean[][] deepCopy(boolean[][] org) {
    if (org == null) {
        return null;
    }

    final boolean[][] res = new boolean[org.length][];
    for (int i = 0; i < org.length; i++) {
        res[i] = Arrays.copyOf(org[i], org[i].length);
     }
    return res;
}
answered Nov 12, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,150 points
55 views
0 votes
1 answer

Join array elements with a separator in Java

Using Java 8, you can easily perform ...READ MORE

answered Aug 16, 2018 in Java by geek.erkami
• 2,320 points
51 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
312 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 25,180 points
129 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,050 points
42 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 7,961 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
1,952 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,078 views
0 votes
1 answer

$ in a variable name in Java

Java compiler uses "$" symbol internally to decorate ...READ MORE

answered Oct 9, 2018 in Java by code.reaper12
• 3,450 points
28 views
0 votes
1 answer

Purpose of “String args[]” in the “psvm” of Java

Let me give you the complete explanation ...READ MORE

answered May 7, 2018 in Java by code.reaper12
• 3,450 points
85 views