String字符串位置移动
有规律的String字符串位置移动
1.自定义一个有规律的String字符串
String numstr = "1,2,3,x,y,4,5";
2.按逗号拆分numstr字符串
String[] nums = numstr.split(",");
3.将nums数组转为list
List<String> numlist = Arrays.asList(nums);
4.获取list中x的下标 此处index为:3
int index = numlist.indexOf("x");
5.将x向后移动一位
Collections.rotate(numlist.subList(index, index+1+1), -1);
6.将list转为String字符串 此处输出numstr为:1,2,3,y,x,4,5
numstr = String.join(",", numlist);
7.rotate方法的使用
Rotate方法需要一个参数distance,该方法将一个List旋转多少长度为distance。假如有个序列列list是[a,b,c,d],调用方法Collections.rotate(list, 1)后,得到的list就变为了[d,a,b,c]。
调用此方法后,位置i上的元素将变为位置(i - distance) mod list.size()的元素,0 <= i < list.size()。distance可以为正数、0、负数。正数代表向前(下标值变大的方向)旋转,负数代表向后旋转。调用方法Collections.rotate(list, -1)后,得到的list就变为了[b,c,d,a]。
这个方法常常和List的subList方法结合使用,用于将一个list的某个或多个元素进行移动,而不破坏其余元素的顺序。例如为了将某个list的位置j的元素向前移动到k的位置。(设移动的距离为d(d >= 0),k = j + d + 1)。
Collections.rotate(list.subList(j, k+1), -1);
举个栗子,例如[a,b,c,d,e],将b元素向前移动三个位置
Collections.rotate(list.subList(1, 5), -1);
调用后得到list为[a,c,d,e,b]。
8.rotate详解
https://blog.csdn.net/u014532901/article/details/52891183
String字符串位置移动的更多相关文章
- Java String字符串/==和equals区别,str。toCharAt(),getBytes,indexOf过滤存在字符,trim()/String与StringBuffer多线程安全/StringBuilder单线程—— 14.0
课程概要 String 字符串 String字符串常用方法 StringBuffer StringBuilder String字符串: 1.实例化String对象 直接赋值 String str=& ...
- [CareerCup] 1.3 Permutation String 字符串的排列
1.3 Given two strings, write a method to decide if one is a permutation of the other. 这道题给定我们两个字符串,让 ...
- iOS - Swift String 字符串
前言 public struct String public class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCodin ...
- 03-Java String字符串详解
1.Java字符串String A.实例化String字符串:直接赋值(更合理一些,使用较多).使用关键字new. B.String内容的比较 // TODO Auto-generated metho ...
- C++学习38 string字符串的增删改查
C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加.删除.更改.查询等操作. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串, ...
- java String字符串——进度1
String字符串 在JAVA中提供了多种创建字符串对象的方法,这里介绍最简单的两种, 第一种是直接赋值, 第二种是使用String类的构造方法: 如下所示: Strin ...
- JavaScript的内置对象(Date日期+string字符串)基础语法总结
1.Date日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 1)定义一个时间对象 : var Udate=new Date(); //注意:使用关键字new,Date()的首 ...
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展字符串位置方法(4)
本文,我们接着之前的框架继续扩展,这次扩展了一共有5个与字符串位置相关的方法 between( left, right ) 返回两个字符串之间的内容, 如果第二个参数没有传递,返回的是找到的第一个参数 ...
- 从零开始学习前端JAVASCRIPT — 3、JavaScript基础string字符串介绍
1:字符串 JS中的任何数据类型都可以当作对象来看.所以string既是基本数据类型,又是对象. 2:声明字符串 基本数据类型:var sStr = '字符串'; 对象的方法:var oStr = n ...
随机推荐
- redHat更新yum源
1. 网易镜像仓库查找相关rpm 包并下载 :http://mirrors.163.com/centos/6/os/x86_64/Packages/ wget http://mirrors.163.c ...
- EntityNotFoundException EntityExistException
package me.zhengjie.common.exception; import org.springframework.util.StringUtils; import java.util. ...
- The 2019 Asia Nanchang First Round Online Programming Contest C(cf原题,线段树维护矩阵)
题:https://nanti.jisuanke.com/t/41350 分析:先将字符串转置过来 状态转移,因为只有5个状态,所以 i 状态到 j 状态的最小代价就枚举[i][k]->[k][ ...
- VisualStudio2010配置使用Halcon
电脑系统环境变量(path): %HALCONROOT%\bin\x86sse2-win32;%HALCONROOT%\bin\x64-win64;%HALCONROOT%\bin\dotnet20; ...
- python练习题——猜数字游戏
增加了按照对半找数的方法来计算最短几次就可以猜到随机数,决定到游戏结束共猜数的次数: from random import * import numpy as np from numpy import ...
- Linux考试试题
mkdir -p /data/oldboy touch lodboy.txt echo "inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255. ...
- [LC] 287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- UFT安装
1.下载解压双击setup.exe 2.点击安装 3.点击下一步 4.检测是否需要安装插件之后一路向下 5.安装之后图标 下载: 链接:https://pan.baidu.com/s/1sa0h037 ...
- Modelsim自动化仿真之do文件书写
创建本地库 vlib ./work You must use vlib rather than operating system commands to creat a library directo ...
- Hello 2015
"Yeah It's on. " 前言 Hux 的 Blog 就这么开通了. 跳过废话,直接看技术实现 2015 年,Hux 总算有个地方可以好好写点东西了. 作为一个程序员, B ...