I'm learning java and my program uses the sorting algorithms. The user can choose the size of the array. I learned to copy an array thanks to this question: Make copy of array Java
I copy the array like this:
public static void main(String[] args) {
int num = Integer.parseInt(JOptionPane.showInputDialog("Size of the array: ?"));
int [] list = new int[num];
for(int i=0;i<num;i++){
int num2 = Integer.parseInt(JOptionPane.showInputDialog("Element: "+(i+1)));
list[i]=num2;
}
int [] listCopy = Arrays.copyOf(list, list.length);
For my bubblesort I used the array saved inside list, for my selection sort I used the array saved inside my listCopy but when I tried to use the insertion sort with listCopy, the array was already sorted.
How can I copy an array so it never changes?