根据Java中的字符列表对单词列表进行排序
我有: 我已经为这段代码写了一些基础,但在我开始认真的“循环”之前,我想问一下还有什么其他方法 谢谢 跟进 我想:根据Java中的字符列表对单词列表进行排序,java,algorithm,sorting,Java,Algorithm,Sorting,我有: 我已经为这段代码写了一些基础,但在我开始认真的“循环”之前,我想问一下还有什么其他方法 谢谢 跟进 我想: 将单词中要排序的每个字母映射到字母表的“倒数”。例如: 'a'是该字符串的第11个字母,因此将其映射到'k' 'b'是该字符串的第24个字母,因此将其映射到'x' 等等 使用集合对列表进行排序。排序 将已排序单词中的每个字母映射回其原始字母,例如'k'->'a';'x'->“b' 我会使用Java8的过滤器和流过滤掉以某个字符开头的内容。。然后我会对结果进行排序。如果需要在单个
是该字符串的第11个字母,因此将其映射到'a'
'k'
是该字符串的第24个字母,因此将其映射到'b'
'x'
- 等等
'k'->'a';'x'->“b'
我会使用Java8的过滤器和流过滤掉以某个字符开头的内容。。然后我会对结果进行排序。如果需要在单个数组中,则合并结果
alphabet(i) compare it with all the words charAt(0)
if only 1 is found i put it to a new list arrangedList
but if 2 is found i go alphabet(i+1) till the letter is found and now i can put them in a right order to arrangedList....
then move back to alphabet(i+1) till alphabet(26) and now all should be arranged correctly...
如果使用索引
,则结果为:
import java.util.Arrays;
/**
* Created by Brandon on 2016-04-17.
*/
public class Main {
public static void main(String[] args) {
String[] array = new String[]{"apple", "stream", "posthouse", "sea", "seed"};
//char[] indices = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] indices2 = new char[]{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
for (char key : indices2) {
String[] result = Arrays.stream(array).filter(value -> value.charAt(0) == key).toArray(length -> new String[length]);
if (result.length > 0) {
Arrays.sort(result);
System.out.println(Arrays.toString(result));
}
}
}
}
[apple]
[posthouse]
[sea, seed, stream]
如果使用指示2
,则结果为:
import java.util.Arrays;
/**
* Created by Brandon on 2016-04-17.
*/
public class Main {
public static void main(String[] args) {
String[] array = new String[]{"apple", "stream", "posthouse", "sea", "seed"};
//char[] indices = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] indices2 = new char[]{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
for (char key : indices2) {
String[] result = Arrays.stream(array).filter(value -> value.charAt(0) == key).toArray(length -> new String[length]);
if (result.length > 0) {
Arrays.sort(result);
System.out.println(Arrays.toString(result));
}
}
}
}
[apple]
[posthouse]
[sea, seed, stream]
在实现comparable的新类中包装字符串怎么样 可能是一些我没有测试过的边缘案例错误
[posthouse]
[apple]
[sea, seed, stream]
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类CompString{
公共静态void main(字符串[]args){
列表=新的ArrayList();
添加(新的可比字符串(“苹果”);
添加(新的可比字符串(“流”);
添加(新的可比字符串(“邮局”);
添加(新的可比字符串(“sea”);
添加(新的可比字符串(“种子”);
集合。排序(列表);
系统输出打印项次(列表);
}
}
类ComparableString实现Comparable{
字符串str;
静态字符串sortOrder=“qwertyuiosapdfghjklzcxvbnm”;
公共可比较字符串(字符串){
str=字符串;
}
@凌驾
公共字符串toString(){
返回str;
}
@凌驾
公共整数比较(可比字符串其他){
对于(inti=0;iother.str.length()){
返回-1;
}else if(this.str.length()
如果我理解正确,那么它只适用于“qwertyuiosapdfghjklzcxvbnm”,但我需要它适用于每个组合编辑:但如果我现在更深入地思考它,那么如果使用字母作为索引并映射到字母,这将起作用谢谢!很抱歉,英语是我的第二语言,是的,这会有用的