0

I'am trying to copy or duplicate an Array without success

   for (int i=0;i<x*y+y;i++)
                {


                    tmpInt = br.read();
                    //Wenn i%x 0 ist dann brakeY eins hochzählen, um damit die Anzahl der Zeilen zu bekommen
                    if (i%x==0 && brakeY<y-1) brakeY++;

                    if (tmpX<=x-1) tmpX++;
                    else tmpX = 0;

                    // Beim ersten Ausführen dieser Teilfunktion wird das Array aus der Textdatei ausgelesen. Beim zweiten Mal jedoch gibt es nur den aktuellen Stand der Map wieder um Veränderungen zu sehen.

                    spielFeld[tmpX][brakeY] = (char) tmpInt; 

                   System.out.print(spielFeld[tmpX][brakeY]);

                //System.out.println("----------");


                }

I'am trying to copy the Array, called spielFeld (german for playground), in this line spielFeldT = spielFeld.clone(); , (spielFeldT = spielFeld didn't work either) so that I can interact with it globally. The results are:

1xwvutsrqpo
2     ü   n
3   !öä   m
4   "     l
5 K §$%   k
789abcdefgh

which is exactly how it's should look like, but if I'am tyring to print the copied array exactly the same way as I printed this one something like this appears.

1     ü   �
3   !öä  �n
4   "   � l
5 K §$%�  k
6     � fgh
789abcdefgh
789abcdefgh
Bruce P
  • 19,995
  • 8
  • 63
  • 73
Hamses
  • 1
  • 1
    Please format your code propperly. You may want to take a look at [Java's `System.arraycopy(...)` method](http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#arraycopy-java.lang.Object-int-java.lang.Object-int-int-). It is expected, that `clone()` does not work since this method is inherited from `Object` and not overwritten (and, therefore, empty). – Turing85 Nov 05 '15 at 19:30
  • 1
    `System.arraycopy()` did not work? http://stackoverflow.com/questions/5785745/make-copy-of-array-java – yogidilip Nov 05 '15 at 19:31
  • I tried " System.arraycopy(spielFeld, 0, spielFeldT, 0,spielFeld.length); ". Still getting an error. – Hamses Nov 07 '15 at 16:56
  • run: 1xwvutsrqpo 2 ü n 3 !öä m 4 " l 5 K §$% k Exception in thread "main" java.lang.NullPointerException 6 & j 789abcdefgh at java.lang.System.arraycopy(Native Method) at project.project.readSpielFeld(project.java:87) at project.project.main(project.java:21) /Users/project:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) – Hamses Nov 07 '15 at 16:57

1 Answers1

0

You can use the System.arraycopy(...) method.

Here is the Syntax of the method,

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

For further information, you may want to take a look at this question.

Community
  • 1
  • 1
Sudha
  • 220
  • 2
  • 16
  • The method is called `System.arraycopy(...)`, not `System.arrayCopy(...)`. Also, you may want to write your code as codeblock, not as text. – Turing85 Nov 05 '15 at 19:34
  • Ok I am new to stack overflow. I will do that – Sudha Nov 05 '15 at 19:36
  • @Sudha would you be so great and change this Method for my purpose? I'am still getting a NullPointerException – Hamses Nov 08 '15 at 09:52
  • @Hames, Can you please provide your code(full program) to check the null pointer exception? – Sudha Nov 09 '15 at 17:05