在Java中更改字符串列表中的特定字符 [英] Changing specific characters in List of Strings in Java
问题描述
背景故事:
List<String> alphabet; // (contains 26 unique characters as elements, for example
// qwertyuiosapdfghjklzcxvbnm)
List<String> wordsToArrange; // contains words as elements, for example:
- apple
- stream
- posthouse
- sea
- seed
我需要根据我制作的字母排列单词。
I need to arrange the words according to the alphabet I have made.
我之前为循环做了很多工作,但是我从上一个问题的答案中得到了一个想法进行映射就像这样:
I was doing huge and many for loops before, but I got an idea from an answer to a previous question to map it like:
alphabet(0) --> "a";
alphabet(1) --> "b";
.................
alphabet(25) --> "z";
所以话会变,苹果会变成苹果---> kjjsc
然后,我可以使用 collection.sort
并创建一个新列表,然后将单词转换回正常状态。.
So the words would change, apple would become apple ---> kjjsc
Then I could use collection.sort
and to a new list, and then convert the words back to normal..
for(int i = 0; i < wordsToArrange.size(); i++){
List<String> charArray = new ArrayList<String>(Arrays.asList(wordsToArrange.get(i).split("")));
int arraySize = wordsToArrange.get(i).length();
Collections.replaceAll(charArray, alphabet.get(0), "a");
Collections.replaceAll(charArray, alphabet.get(1), "b");
Collections.replaceAll(charArray, alphabet.get(2), "c");
Collections.replaceAll(charArray, alphabet.get(3), "d");
Collections.replaceAll(charArray, alphabet.get(4), "e");
Collections.replaceAll(charArray, alphabet.get(5), "f");
Collections.replaceAll(charArray, alphabet.get(6), "g");
Collections.replaceAll(charArray, alphabet.get(7), "h");
Collections.replaceAll(charArray, alphabet.get(8), "i");
Collections.replaceAll(charArray, alphabet.get(9), "j");
Collections.replaceAll(charArray, alphabet.get(10), "k");
Collections.replaceAll(charArray, alphabet.get(11), "l");
Collections.replaceAll(charArray, alphabet.get(12), "m");
Collections.replaceAll(charArray, alphabet.get(13), "n");
Collections.replaceAll(charArray, alphabet.get(14), "o");
Collections.replaceAll(charArray, alphabet.get(15), "p");
Collections.replaceAll(charArray, alphabet.get(16), "q");
Collections.replaceAll(charArray, alphabet.get(17), "r");
Collections.replaceAll(charArray, alphabet.get(18), "s");
Collections.replaceAll(charArray, alphabet.get(19), "t");
Collections.replaceAll(charArray, alphabet.get(20), "u");
Collections.replaceAll(charArray, alphabet.get(21), "v");
Collections.replaceAll(charArray, alphabet.get(22), "w");
Collections.replaceAll(charArray, alphabet.get(23), "x");
Collections.replaceAll(charArray, alphabet.get(24), "y");
Collections.replaceAll(charArray, alphabet.get(25), "z");
StringBuilder listString = new StringBuilder();
for (String s : charArray)
listString.append(s+"");
System.out.println(listString);
这似乎会覆盖所有字符,因此输出为:
This seems to overwrite all the characters so the output is:
rqqsw
sezwrz
qisepiosw
swr
swwz
今天我的大脑雾气很大,我想我可以将这种方法推到最后,直到我找到某种解决方案为止,但这似乎并不是有效的解决方法做吧。有什么想法吗?
My brain is quite foggy today and I think I could push this way to the end till I have some kind of solution, but this does not seem like the efficient way to do it. Any ideas?
此刻,我从列表中取出一个单词,将其放入一个char数组中,这样我就可以分别更改字符,然后将新的char数组放回去字符串,然后我将其在列表中替换为一个新单词(或创建一个新列表)。仔细阅读所有单词后,我将得到一个列表。我会在其上使用 collection.sort
,然后再转换回普通单词,但现在顺序正确。
At the moment I take a word from a list, make it into a char array so I could change very character separately, then put the new char array back to string, and then I would replace it in the list with a new word (or make a new list). After I have gone through all the words I would have a list. I would use collection.sort
on it and then convert back to normal words, but now in correct order.
我的第一个问题:
根据Java中的字符列表对单词列表进行排序
推荐答案
问题是您所使用的字符
最简单的方法是通过一次更改字符。
The simplest approach is to change the characters in a single pass.
public static String encode(String input, String dict) {
StringBuilder sb = new StringBuilder();
for (char ch : input.toCharArray()) {
int index = dict.indexOf(ch); // look for the character
if (index >= 0)
sb.append((char) ('a' + index)) // use the matching char instead
else
sb.append(ch); // otherwise copy untranslated.
}
return sb.toString();
}
这篇关于在Java中更改字符串列表中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!