This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 41
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thanks so much! Very much appreciated!
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thanks!
 
Greenhorn
Posts: 19
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!