String string position move

Source: Internet
Author: User

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

RotateMethod requires a parameterdistance, the method adds aListHow many lengths are rotateddistance。 If there is a sequence columnlistIs[a,b,c,d], call methodCollections.rotate(list, 1)After that get thelistIt changed to[d,a,b,c]
When this method is called, the locationiThe element on will become a position(i - distance) mod list.size()The Elements,0 <= i < list.size()distanceCan 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 thelistIt 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.