gpt4 book ai didi

java - 旋转列表<字符>并维护字数

转载 作者:行者123 更新时间:2023-12-02 10:34:09 25 4
gpt4 key购买 nike

我试图将字符串逐行旋转到右侧(输入来自文件)。输入可能有很多行,因此需要每行进行旋转。

例如,将其向右旋转 5 and line breaks^$@ 最终会是:^$@ and line breaks .

我正在使用List<String>并做了以下工作:

 private static List<String> rflag(String value, List<String> lines) {
List<String> newLines = new ArrayList<>();

int rvalue = Integer.parseInt(value);

for (String line : lines) {
StringBuilder sb = new StringBuilder();

if (!line.isEmpty()) {
List <Character> chars= new ArrayList<>();

for(char ch: line.toCharArray()){
chars.add(ch);
}

Collections.rotate(chars, rvalue);

sb.append(chars);
String text = sb.toString()
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "") //remove the left bracket
.trim();
newLines.equals(text);
}
}
return newLines;
}

如果我输入类似 abcXYZ 的内容,我的输出最终是 [b, c, X, Y, Z, a]bcXYZa删除括号和逗号。

我的主要问题是,虽然我可以删除括号和逗号,但我不会保留输入中的行或单词。

最佳答案

改变

.replace(",", "")  //remove the commas

.replace(", ", "")  //remove the commas

使此代码按您的预期工作。

System.out.println(rflag("2", Arrays.asList("and line breaks"))); //output: ksand line brea

(和换行符^$@ ,当前输出是^ $ @ a n d l i in e b e a k s,但应该是^$@和换行符 .)

关于java - 旋转列表<字符>并维护字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com