Using Looping Construct to Copy Arrays

Let's take an example:


\begin{lstlisting}
import java.util.Arrays;
\par
class Main {
public static voi...
... string
System.out.println(Arrays.toString(destination));
}
}
\end{lstlisting}

The output for the above code block looks like [1, 2, 3, 4, 5, 6]. In the above example, we have used the for loop to iterate through each element of the source array. In each iteration, we are copying elements from the source array to the destination array. Here, the source and destination array refer to different objects (deep copy). Hence, if elements of one array are changed, corresponding elements of another array is unchanged. Notice the statement, System.out.println(Arrays.toString(destination)); Here, the toString() method is used to convert an array into a string.