This discussion is archived
8 Replies Latest reply: Jan 4, 2007 9:15 PM by 807599 RSS

reversing the order of an array

807599 Newbie
Currently Being Moderated
i am new to arrays. i was told to reverse the order of an array. i knew i had to use a for loop and i need to use array.length in the for loop. so i cannot really figure it out how to reverse an order of an array. i google and got the solution but i can't understand the for loop at all.
class Exercise3
{
  public static void main ( String[] args )
  {
   int[] val = {0, 1, 2, 3}; 
    int temp;
 
    System.out.println( "Original Array: " 
        + val[0] + " " + val[1] + " " + val[2] + " " + val[3] );
 
   for(int j = 0; j<(val.length/2); j++){ //length is 4 so 4/2 = 2 which is true and y do i have to divide the length by 2? i deleted the /2 and then it prints the same array but the length of an array is 4 which is less than 0 so y do i have to divide it by 2?
               temp = val[j]; //assign val[] to temp
               val[j] = val[val.length-1-j]; //val[0] = val[4-1-0] which is 3 for the first iteration of the for loop.
               val[val.length-1-j] = temp; // i can't understand this assignment statement temp is holding the values of array }
    System.out.println( "Reversed Array: " 
        + val[0] + " " + val[1] + " " + val[2] + " " + val[3] );
       
   }
}
Message was edited by:
fastmike

Message was edited by:
fastmike
  • 1. Re: reversing the order of an array
    807599 Newbie
    Currently Being Moderated
    Well rather than copying code you don't understand why don't you start writing your own?

    Have your starting array.

    Create a new array of the same length

    Grab the elements from the first array in ascending order

    Insert these elements into the second array in descening order.
  • 2. Re: reversing the order of an array
    807599 Newbie
    Currently Being Moderated
    i know exactly what you saying i could have done it easily by creating another array;
    int[] reverse = new int[4];
    reverse[0] = val[3];
    ........
    System.out.println(The reverse array " + reverse[0] + "...");
    But since i had to use that temp. i can't figure it out so rather than wasting your guys time i searched it on google and try to see if i can understand the logic behind but did not
  • 3. Re: reversing the order of an array
    807599 Newbie
    Currently Being Moderated
    i know exactly what you saying i could have done it
    easily by creating another array;
    And I think you might try that and put it into a for loop and see if you see anything. It might explain about the other.

    The other... which I will try and explain now goes like this.

    It's reversing the array "in place". So to do this you take one element (the first or last) and swap it with the other. You keep doing this until you get to the mid way point of the array. (Can you think why you should stop at the midway point?)

    For each swap you need to have a temporary variable to hold one of the values. As a simple example look at this.
    int[] a = // array with two ints
    // swap the two values
    int temp = a[0];
    a[0] = a[1];
    a[1] = temp;
    You see how we can copy one value directly but we need one temp variable to hold the other value while we swap?
  • 4. Re: reversing the order of an array
    807599 Newbie
    Currently Being Moderated
    It's reversing the array "in place". So to do this
    you take one element (the first or last) and swap it
    with the other. You keep doing this until you get to
    the mid way point of the array. (Can you think why
    you should stop at the midway point?)
    That is what my whole question was y do we have to stop at the midpoint? nvm. i will try to write this code in a different way lets see if i can get successful which i know i will and i will post back. i was just getting confused with that midpoint thing.
    Thanks
  • 5. Re: reversing the order of an array
    807599 Newbie
    Currently Being Moderated
    It's reversing the array "in place". So to do this
    you take one element (the first or last) and swap
    it
    with the other. You keep doing this until you get
    to
    the mid way point of the array. (Can you think why
    you should stop at the midway point?)
    hat is what my whole question was y do we have to
    stop at the midpoint?
    I thought your question was about the temp variable.

    Anyway consider the following.

    original array
    1,2,3,4,5,6

    After 1st Swap (swap elements 0 and 5)

    6,2,3,4,5,1

    After 2nd Swap (swap elements 1 and 4)

    6,5,3,4,2,1

    After 3rd Swap (swap elements 2 and 3)

    6,5,4,3,2,1

    After 4th Swap (swap elements 3 and 2)

    6,5,3,4,2,1

    After 5th Swap (swap elements 4 and 1)

    6,2,3,4,5,1

    After 6th swap (swap elements 5 and 0)

    1,2,3,4,5,6

    You may have noticed something here
  • 6. Re: reversing the order of an array
    807599 Newbie
    Currently Being Moderated
    got it we had to stop at the midpoint because total length is 4 but the array index is 3(0,1,2,3).
  • 7. Re: reversing the order of an array
    807599 Newbie
    Currently Being Moderated
    got it we had to stop at the midpoint because total
    length is 4 but the array index is 3(0,1,2,3).
    I don't know what you mean here. Did you look at my example?

    The key in understanding is that for each swap you are reversing TWO
    elements and not just one. So going through the entire array will reverse
    it and then re-reverse it. Which is probably not desirable.
  • 8. Re: reversing the order of an array
    807599 Newbie
    Currently Being Moderated
    mr cotton you are a lifesaver lol i get excited. i came to realize this before i saw your previous post of explaning the swaps but that was grea. Thanks