I was simply doing a test to see the difference between a for-each loop and a regular for loop. I initialized an array and reversed it, and then tried to print it with both loops, however, only the regular for loop printed out the reversed array.
public static void main(String[] args) {
int[] list1 = {0,1,2,3,4,5,6,7,8,9};
int[] list2 = reverse(list1);
for(int i=0;i<list2.length;i++) {
System.out.print(list2[i]);
}
System.out.println();
for(int i:list2) {
System.out.print(list2[i]);
}
}
static int[] reverse(int[] a) {
int[] temp = new int[a.length];
for(int i=0,j=a.length-1;i<a.length;i++,j--) {
temp[i]=a[j];
}
return temp;
}
I tried using the Arrays.toString method of the Arrays class to print out the list, and it also prints out the reversed list, both before and after the for-each loop.