aspose file tools*
The moose likes Beginning Java and the fly likes How to move array item from top of list to bottom of list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to move array item from top of list to bottom of list" Watch "How to move array item from top of list to bottom of list" New topic
Author

How to move array item from top of list to bottom of list

M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 32
I don't need to sort. I need to know how to move first item in the list to the bottom of the list.

String inputString = "alpha|beta|gamma|delta";
String tokens[] = inputString.split("\\|");
int numTokens = tokens.length;

for (int i = 0; i < numTokens; i++) {
System.out.println("[" + i + "] " + tokens[i]);
}

Output from syst out print:
[0] alpha
[1] beta
[2] gamma
[3] delta

I would like to know how to take the result of Output and move the first to always be at the bottom of the list?
[0] beta
[1] gamma
[2] delta
[3] alpha

I know that tokens[0] will always be the same value for this example it will always be "alpha", it will always be in the first position in the string inputString, but I don't know how to move it to bottom of list if possible.

Any suggestion appreciated Thank you very much.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

1. Set all values in a new array (loop)
2. Take first value
3. Take last value
4. Put first value in last place
5. Put last value in first place

You could do everything in step 1 too:
1. Set all values in a new array. If i==0, set value to last place. If i==numTokens-1 set value to first place.

[My Blog]
All roads lead to JavaRanch
M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 32
I get what you're saying though I do not want to switch the last value to first place, want to just move the rest up in one position after I move the first one to the last place. Thanks!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Sorry, I don't know why I was thinking you wanted to swap first and last
You can still do it in one loop:
1. Create array of length=numTokens
2. Loop through values
2-1. If i==0, set value to last place.
2-2. Else set value at i-1 place
M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 32
Great! Thanks so much! Very much appreciated!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There's a static method "rotate()" in java.util.Collections that you could use:

String[] strings = ...
Collections.rotate(Arrays.asList(strings), -1);

This elegant trick works because Arrays.asList() returns a list that "wraps" the given array; changes to that list end up back in the original array.

[Jess in Action][AskingGoodQuestions]
M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 32
Great! Thanks!
Steve Harney
Greenhorn

Joined: Jun 19, 2010
Posts: 19
You could also use System.arraycopy, this array will roll multiple spots, if you only need to move it one spot you could get rid of they top and bottom copies.

M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 32
Thanks!!!
 
Don't get me started about those stupid light bulbs.
 
subject: How to move array item from top of list to bottom of list