Regular string string position movement
1. Customizing a regular string string
" 1,2,3,x,y,4,5 ";
2. Splitting NUMSTR strings by commas
string[] Nums = Numstr.split (",");
3. Convert the Nums array to list
list<string> numlist = arrays.aslist (nums);
4. Get the subscript for X in list here index: 3
int index = numlist.indexof ("x");
5. Move x Backward by one
Collections.rotate (numlist.sublist (index, index+1+1),-1);
6. Convert list to string string here output Numstr: 1,2,3,y,x,4,5
Numstr = String.Join (",", numlist);
use of the 7.rotate method
Rotate
Method requires a parameterdistance
, the method adds aList
How many lengths are rotateddistance
。 If there is a sequence columnlist
Is[a,b,c,d]
, call methodCollections.rotate(list, 1)
After that get thelist
It changed to[d,a,b,c]
。
When this method is called, the locationi
The element on will become a position(i - distance) mod list.size()
The Elements,0 <= i < list.size()
。distance
Can be positive, 0, negative. Positive numbers rotate forward (the direction in which the subscript value becomes larger), and negative numbers represent backward rotation. Calling methodsCollections.rotate(list, -1)
After that get thelist
It changed to[b,c,d,a]
。
This method is often List
used in conjunction with a subList
method that moves list
one or more elements without destroying the order of the remaining elements. For example, to list
move an element of a position j
forward to a k
position. (Set the distance to move is d
( d >= 0
) k = j + d + 1
,).
Collections.rotate(list.subList(j, k+1), -1);
To raise a chestnut, for example [a,b,c,d,e]
, b
move an element forward three positions
Collections.rotate(list.subList(1, 5), -1);
After the call list
is [a,c,d,e,b]
received.
8.rotate explanation
52891183
String string position move