Results 1 to 4 of 4
  1. #1
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default How to I change the position in arrays

    Alright, so what I want to do is presenting an array backward... so if I have an array of length 10 (1,2,3,4,5...) I want them to come out as 10, 9, 8...

    I was thinking something along these lines:

    public class uppg1 {
    int[] array= {1,2,3,4,5,34,7,8,9,10};
    int i;

    public void summa(){

    for (i=0; i<array.length; i++);{
    for (int j=i+10; j<array.length; j++)
    {
    int tmp=array[i];
    int array[i]=array[j];
    array[j]=tmp;
    }

    }
    }


    buuut it doesnt seem to work, any tip? :)

    regards, lenny

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    You don't need a nested loop to reverse an array and btw the loop boundaries of your inner loop suck big times ;-)

    Java Code:
    for (int i= 0; i < array.length; i++) {
       int j= array.length-1-i;
       int tmp= array[i]; array[i]= array[j]; array[j]= tmp;
    }
    kind regards,

    Jos

  3. #3
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You don't need a nested loop to reverse an array and btw the loop boundaries of your inner loop suck big times ;-)

    Java Code:
    for (int i= 0; i < array.length; i++) {
       int j= array.length-1-i;
       int tmp= array[i]; array[i]= array[j]; array[j]= tmp;
    }
    kind regards,

    Jos
    dude, I SUCK :D but im working on it

    thanks mate, have a nice night

    //lenny

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    You don't need a nested loop to reverse an array and btw the loop boundaries of your inner loop suck big times ;-)

    Java Code:
    for (int i= 0; i < array.length; i++) { // array.length / 2, perhaps?
       int j= array.length-1-i;
       int tmp= array[i]; array[i]= array[j]; array[j]= tmp;
    }
    kind regards,

    Jos
    Hmmm... see comment in code

    db

Similar Threads

  1. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  2. Change the Font Size and Text Position
    By stayfierce in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 06:17 AM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 02:36 AM
  4. BorderFactory to set position?
    By aleplgr in forum AWT / Swing
    Replies: 2
    Last Post: 01-30-2008, 01:46 PM
  5. Use the mouse position
    By susan in forum Java Applets
    Replies: 1
    Last Post: 07-29-2007, 12:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •