0
for (int i = 1; i < list.length; i++){
        list[i-1] = list[list.length-i]; }

As you can see, when my list reaches middle it has already new stored digits and uses the new ones. So by typing 1,2,3,4,5, come ous 5,4,3,4,5... so how would you swap elements in an array without creating a new array?

pigi
  • 45
  • 1
  • 4
  • You need to use an extra variable for swapping. – PM 77-1 Dec 26 '15 at 18:43
  • Create a new array (e. g. `int[] reversed = new int[list.length]`, loop through the first array using indexes, and then use the index as the difference from the end of the array (e. g. `reversed[i] = list[list.length-1-i];`) – Momo Dec 26 '15 at 18:46
  • Well how to do it without a new array ) – pigi Dec 26 '15 at 19:06

0 Answers0