I need to check if the elements in the arraylist are arranged in ascending order or not. and when i use this code, the output shows "not sorted" even for a sorted array. why is it showing the wrong output?**
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> array1 = new ArrayList<>();
ArrayList<Integer> array2 = new ArrayList<>();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
array1.add(i, sc.nextInt());
array2.add(i, array1.get(i));
}
Collections.sort(array1);
if (array1 == array2) {
System.out.println("sorted");
}
else {
System.out.println("not sorted");
}
}