File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
M Wilson
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
M Wilson
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thanks so much! Very much appreciated!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
M Wilson
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thanks!
 
Steve Harney
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic