Copying Arrays Using arraycopy() method

In Java, the System class contains a method named arraycopy() to copy arrays. This method is a better approach to copy arrays than the above two. The arraycopy() method allows you to copy a specified portion of the source array to the destination array. For example,


\begin{lstlisting}
arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
\end{lstlisting}

Here,

Let's take an example:


\begin{lstlisting}
// To use Arrays.toString() method
import java.util.Arrays;
\...
...2);
System.out.println(''n3 = '' + Arrays.toString(n3));
}
}
\end{lstlisting}

Output for the above program can be


\begin{lstlisting}
n2 = [2, 3, 12, 4, 12, -2]
n3 = [0, 12, 4, 0, 0]
\end{lstlisting}

In the above example, we have used the arraycopy() method,

As you can see, the default initial value of elements of an int type array is 0.