-1
    public static String[][] deepCopy(String[][]toclone){
    String[][]clone = new String[4][4];
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 4; j++) {
            if(toclone[i][j] != null) {
                String s = new String(toclone[i][j]);
                clone[i][j] = s;
            }
        }
    }
    return clone;
}

I need a deep copy and not just a flat one for my programm..clone() does not work for my problem. Thanks already for your tips and advice.This questions wasnt answered before or atleast not with the question linked with an 2d int array deep copy. That does not help me at all.

Heiko
  • 55
  • 5
  • Forgot to mention: thats how far i got, but sadly my solution does not work out for me. :/ – Heiko Jan 17 '19 at 20:34

1 Answers1

-2

You can use either of these:

  1. Arrays.copyOf()
  2. System.arraycopy()
  3. Object clone() method

If you are looking for more advanced library with cloning api's you can check

org.apache.commons.lang3 SerializationUtils

Ratish Bansal
  • 1,982
  • 1
  • 10
  • 19
  • Thanks for the answer. I tried your solutions, but neither of them worked for me. Maybe i am doing it in the wrong way, Would you be so kind and write a piece of code how to use your solutions. I am still a bloody beginner. – Heiko Jan 17 '19 at 20:53
  • String myTwoDimensionalStringArray[][] = {{"Apple", "Banana"}, {"Pork", "Beef", "Chicken"}, {"Carrots"}}; String clonedArray[][] = Arrays.copyOf(myTwoDimensionalStringArray, myTwoDimensionalStringArray.length); – Ratish Bansal Jan 17 '19 at 21:02