• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
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 Ubuntu VI Editor
  • 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 Ubuntu VI Editor
  • 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
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • 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!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!