java: why does this segment of code reverse the order of the elements of an array?

Discussion in 'Mac Programming' started by macman2790, Feb 14, 2007.

  1. macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #1
    i'm reviewing for a test and this is about the only thing that's bothering me.
    heres the code:
    Code:
    //Write a code segment that reverses the elements in the integer array, a.
    int temp;
    for (int i=0;i<a.length/2;i++)
       temp = a[i];
       a[i] = a[a.length - i - 1];
       a[a.length - i - 1] = temp;
    
    
    
    
     
  2. macrumors 68040

    iW00t

    Joined:
    Nov 7, 2006
    Location:
    Defenders of Apple Guild
    #2
    Yeah it is intriguing...

    Why is it looped up to a.length/2?
     
  3. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #3
    Is that your own code or from a teacher or book? Since all that does is set temp, and is equivalent to:
    Code:
    int temp;
    for (int i=0;i<a.length/2;i++) {
       temp = a[i];
    }
    a[i] = a[a.length - i - 1];
    a[a.length - i - 1] = temp;
    In other words, you (or your teacher or text book) will have to be more careful about defining blocks of code.

    The following makes more sense with your question:
    Code:
    int temp;
    for (int i=0;i<a.length/2;i++) {
       temp = a[i];
       a[i] = a[a.length - i - 1];
       a[a.length - i - 1] = temp;
    }
    If you don't understand how this works, do it on paper, with the array a going from 0 to 9 so you have the loop going from 0 to 5.
     
  4. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #4
    no idea, that's one of the things that's killing me, but another part i don't understand is the is the part to the right of it which is that -i -1. yeah it's my teacher, i know, i have to deal with it everyday. i see what you mean about the block
     
  5. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #5
    I'm not quite sure how to do it on paper, the only thing i see is the 0 to 5 thing.
     
  6. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #6
    Doing a of length 6 for simplicity, write the following on a piece of paper
    Code:
            start     i = 0     i = 1
    a[0]      0         5         ?
    a[1]      1         1
    a[2]      2         2
    a[3]      3         3
    a[4]      4         4
    a[5]      5         0
    
    step 0, i = 0, a.length - i - 1 = 5
    -------------------------------------
    temp set to a[0] = 0
    a[i] is a[0], set to a[5], ie. a[0] = 5
    a[a.length - i - 1] is a[5], set to temp, ie. a[5] = 0
    
    step 1, i = 1, a.length - i - 1 = 4
    -------------------------------------
    temp set to a[1] = 1
    etc
    
    Do the rest yourself. Continue the loop to a.length/2 (i.e. 3)
     
  7. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #7
    thanks, you were a big help.
     
  8. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #8
    plinden - you are thorough!

    macman, if you haven't noticed, that algorithm walks through the first half of the array swapping elements.

    1st pass - swaps 1st and last element
    2nd pass - swaps 2nd and 2nd to last element
    and so on.

    If you traverse the entire array (instead of only half) you would simply end up with the original array.
     

Share This Page