Results 1 to 3 of 3
  1. #1
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Array Sort Question

    Ok if I have an array that looks like this:{3,4,3,2,7,5,2,1}

    I want to split the numbers in the array into sums of 9. So I start with the first index of the array and add it to the second, third, fourth, etc...until I get 9. If a total that I get is greater than 9 (e.g. 3 + 4 + 3 is greater than 9) then I want the current index to be moved to the end of the array and the for the other indices to be shifted left.

    For example with my given array:
    3 + 4 = 7 + 3 = 10.
    So 3 needs to be sent to the end of the array pushing the other indices forward, like so: {3,4,2,7,5,2,1,3}

    How would I do something like that? I'm not opposed to a Collections.swap approach. Below is a start of what I am try to attempt:
    Java Code:
    int temp = 0, temp2 = 0;
    for(int i = frontPointer; i < backPointer + 1; i++){
         for(int j = backPointer; j > frontPointer; j--){ 
             temp = item.get(j);
             temp2 = item.get(i);
             item.set(i, item.get(i + 1));
             item.set(j, temp2);
             item.set(j-1, temp);
         }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Array Sort Question

    I want the current index to be moved to the end of the array and the for the other indices to be shifted left.
    Remove the element at what you regard as the "current" position and add it again. (This assumes that item is a List that supports these operations.)

  3. #3
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Array Sort Question

    Yeah, that's what I ended up doing. Gotta love the ArrayList =]

Similar Threads

  1. heap sort question
    By stuckonjava in forum New To Java
    Replies: 18
    Last Post: 03-15-2012, 04:18 PM
  2. Need help with Array.sort()
    By Kinney.j in forum New To Java
    Replies: 1
    Last Post: 10-16-2011, 07:51 AM
  3. Selection sort question
    By eoins2345 in forum New To Java
    Replies: 11
    Last Post: 08-25-2011, 06:14 AM
  4. Question with bubble sort
    By Metastar in forum New To Java
    Replies: 22
    Last Post: 09-13-2010, 07:25 AM
  5. Array sort
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 12:41 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
  •