# Array Sort Question

• 04-17-2012, 11:11 PM
kraigballa
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:
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);     } }```
• 04-17-2012, 11:30 PM
pbrockway2
Re: Array Sort Question
Quote:

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.)
• 04-18-2012, 12:14 AM
kraigballa
Re: Array Sort Question
Yeah, that's what I ended up doing. Gotta love the ArrayList =]