1. Senior Member
Join Date
Oct 2011
Posts
115
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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. Senior Member
Join Date
Oct 2011
Posts
115
Rep Power
0

## Re: Array Sort Question

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

#### Posting Permissions

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