背景故事:
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
我需要根据我制作的字母排列单词。
之前我做了很多很多for循环,但I got an idea from an answer to a previous question将它映射为:
alphabet(0) --> "a";
alphabet(1) --> "b";
.................
alphabet(25) --> "z";
所以这些话会改变,苹果会变成苹果---&gt; kjjsc
然后我可以使用collection.sort
和新列表,然后将单词转换回正常..
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);
这似乎覆盖了所有字符,因此输出为:
rqqsw
sezwrz
qisepiosw
swr
swwz
我的脑子今天很大雾,我想我可以推动这种方式直到我有一些解决方案,但这似乎不是有效的方法。有什么想法吗?
目前我从列表中取出一个单词,将其变成一个char数组,这样我就可以单独更改字符,然后将新的char数组放回字符串,然后我将用新的替换它在列表中单词(或创建一个新列表)。在我完成所有单词后,我会有一个列表。我会在其上使用collection.sort
,然后转换回正常的单词,但现在按正确的顺序。
我的第一个问题: Sorting list of words according to list of characters in Java
答案 0 :(得分:4)
问题在于您替换它们的字符也是您稍后要替换的字符。
最简单的方法是一次性更改字符。
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();
}