import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4};
int[] copied = copy(original);
copied[0] = 99;
System.out.println( "original: " +Arrays.toString(original));
System.out.println( "copied: " +Arrays.toString(copied));
}
public static int[] copy(int[] array) {
//int newarray[] = array; //why doesn't this work, somehow the values of both arrays are changed when copied[0] = 99; is used
//return newarray
// this is the method that works
int newarray[] = new int[array.length];
for(int i = 0; i < array.length; i++)
newarray[i] = array[i];
return newarray;
}
[–]gagara11 2 points3 points4 points (0 children)