Java crew, having trouble with this method I wrote

Discussion in 'OT Technology' started by cheet, May 9, 2008.

  1. cheet

    cheet OT Supporter

    Joined:
    Mar 22, 2004
    Messages:
    13,235
    Likes Received:
    3
    Location:
    CA
    I have this method that swaps ther values in two positions in an array. It doesn't appear to swap anything at all, the array is left in the same state. How can I make this right?

    Code:
    [LEFT][B][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/B][SIZE=2] swap([/SIZE][B][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/B][SIZE=2] toSwap, [/SIZE][B][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/B][SIZE=2] swapWith){[/SIZE]
    [SIZE=2]E temp = [/SIZE][SIZE=2][COLOR=#0000c0]pq[/COLOR][/SIZE][SIZE=2][toSwap];[/SIZE]
    [SIZE=2][COLOR=#0000c0]pq[/COLOR][/SIZE][SIZE=2][toSwap] = [/SIZE][SIZE=2][COLOR=#0000c0]pq[/COLOR][/SIZE][SIZE=2][swapWith];[/SIZE]
    [SIZE=2][COLOR=#0000c0]pq[/COLOR][/SIZE][SIZE=2][swapWith] = temp;[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
    
    The method is passed the first position and the second position of what is to be swapped. So in the array:

    pq[0]=How
    pq[1]=are
    pq[2]=you

    if toSwap = 1, and swapWith = 2 then the result should be

    pq[0]=How
    pq[1]=you
    pq[2]=are

    edit- lol im an idiot, it works fine
     
    Last edited: May 10, 2008
  2. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Is pq a global array or is it a member of a class?
     
  3. cheet

    cheet OT Supporter

    Joined:
    Mar 22, 2004
    Messages:
    13,235
    Likes Received:
    3
    Location:
    CA
    global
     
  4. pirothezero

    pirothezero New Member

    Joined:
    Dec 4, 2007
    Messages:
    127
    Likes Received:
    0
    Location:
    Austin,TX
    is pq or the method static?

    works for me.
     
    Last edited: May 10, 2008
  5. cheet

    cheet OT Supporter

    Joined:
    Mar 22, 2004
    Messages:
    13,235
    Likes Received:
    3
    Location:
    CA
    Ok, other parts of my class were messed up...it does work
     
  6. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    Looks like it works great already

    Edit*
    Just saw your message. :hs:
     

Share This Page