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 13, 2018 in Java by 93.lynn
• 1,600 points
8,455 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;
}

Hope it helps!!

If not then its recommended to join our Java training class and learn about Java in detail.

Thank You!!

answered Nov 13, 2018 by code.reaper12
• 3,500 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,190 points
886 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,680 points
2,125 views
0 votes
1 answer

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,910 points
999 views
0 votes
0 answers

how to sort 2d array in java ?

Nov 29, 2023 in Java by Evanjalin
• 360 points
243 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,120 points
975 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 79,578 views
+1 vote
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,500 points
7,967 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,500 points
16,996 views
0 votes
1 answer

$ in a variable name in Java

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

answered Oct 10, 2018 in Java by code.reaper12
• 3,500 points
5,664 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,500 points
4,286 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP