我正在尝试将字符串String input;
转换为莫尔斯代码并返回。我很难搞清楚使用哪种方法以及如何使用它。字符串我正在尝试转换为莫尔斯代码:SOS
,应该转换为...---...
,从莫尔斯代码转换为英语:...---...
- SOS
。
我试过的一种方法是使用两个数组String[] alpha = {A-Z0-9}
和String[] morse = {morse patterns}
。然后我尝试将String
输入拆分为数组,将String输入中的每个字符与String[] alpha
中的每个字符进行比较,并将每个索引存储在indexArray[]
中。我使用了inputArray= input.split("", -1);
最后使用一些for
循环和if
语句,我尝试使用我想要找到的字符串字符的索引,以便在String[] morse
中找到莫尔斯代码。
我上面尝试的内容不适用于单词,但适用于一个字符(下面的代码)。它失败了,我无法弄清楚如何以这种方式修复它。这甚至是最好的方法吗?或HashMap
?
然后我尝试使用HashMap
,其中英文字符为关键字,莫尔斯为值。
哪种方式是将英文字符串转换为摩尔斯电码和摩尔斯电码到英文字符串的最佳方法?数组或HashMap?
阵列:
private String[] alpha = {"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", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "0", " "};
private String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|"};
我正在努力打破循环,无法弄清楚如何做到这一点:
public int[] indexOfArray(String englishInput) {
englishArray = englishInput.split("", -1);
for (int j = 0; j < englishArray.length; j++) {
for (int i = 0; i < alpha.length; i++) {
if (alpha[i].equals(englishArray[j])) {
indexArray[i] = i;
}
}
}
return indexArray;
}
这仅适用于一个字符(字符与莫尔斯代码):
public int indexOfArrayOld(String englishInput) {
for (int i = 0; i < alpha.length; i++) {
if (alpha[i].equals(englishInput)) {
indexOld = i;
}
}
return indexOld;
}
public String stringToMorseOld(int dummyIndex) {
String morseCo = morse[dummyIndex];
return morseCo;
}
HashMap中:
private static HashMap<String, String>; alphaMorse = new HashMap<String, String>();
static {
alphaMorse.put("A", ".-");
alphaMorse.put("B", "-...");
alphaMorse.put("C", "-.-.");
alphaMorse.put("D", "-..");
alphaMorse.put("E", ".");
alphaMorse.put("F", "..-.");
alphaMorse.put("G", "--.");
alphaMorse.put("H", "....");
alphaMorse.put("I", "..");
alphaMorse.put("J", ".---");
alphaMorse.put("K", "-.-");
alphaMorse.put("L", ".-..");
alphaMorse.put("M", "--");
alphaMorse.put("N", "-.");
alphaMorse.put("O", "---");
alphaMorse.put("P", ".--.");
alphaMorse.put("Q", "--.-");
alphaMorse.put("R", ".-.");
alphaMorse.put("S", "...");
alphaMorse.put("T", "-");
alphaMorse.put("U", "..-");
alphaMorse.put("V", "...-");
alphaMorse.put("W", ".--");
alphaMorse.put("X", "-..-");
alphaMorse.put("y", "-.--");
alphaMorse.put("z", "--..");
alphaMorse.put("1", ".----");
alphaMorse.put("2", "..---");
alphaMorse.put("3", "...--");
alphaMorse.put("4", "....-");
alphaMorse.put("5", ".....");
alphaMorse.put("6", "-....");
alphaMorse.put("7", "--...");
alphaMorse.put("8", "---..");
alphaMorse.put("9", "----.");
alphaMorse.put("0", "-----");
alphaMorse.put(" ", "|");
}
答案 0 :(得分:1)
我认为,理想情况下你有一个二维数组,如:
String[][] morseArray = new String[][] {{ "S" , "..." }, { "O", "---" }};
然后,如果你正在寻找速度&amp;易于查找,您可能希望有两个地图:
private Map<String,String> enToMorse;
private Map<String,String> morseToEn;
以及一些检索它们的私有方法:
private Map<String,String> getMorseToEnMap() {
if(morseToEn==null) {
morseToEn = new HashMap<String,String>();
for(String[] x : morseArray) {
morseToEn.put(x[1], x[0]);
}
}
return morseToEn;
}
然后你可以去:
Map<String,String> morse = getMorseToEn();
String x = morse.get("...");
[...]
好处是:一方面,您可以轻松地定义映射 - 两个独立的阵列更难以保持同步 - 而另一方面,地图将是最快速,最简单的方式查找这样的东西。
答案 1 :(得分:1)
使用Hashmap并获取方法来比较它们,最后将键或值附加到字符串:
public String stringToMorse(String str){
String morse = "";
for(char s: str.toCharArray()){
morse += (String) Hashmap.get(s)+" ";
}
return morse;
}
对于另一个使用此方法而不是get():
public static Object getKeyFromValue(Map hm, Object value) {
for (Object o : hm.keySet()) {
if (hm.get(o).equals(value)) {
return o;
}
}
return null;
}
答案 2 :(得分:0)
效率方面我会推荐并列数组结构。 我做了一些这些方法的演示,我不是一个效率向导所以嵌套循环在这里可能不太理想,或者我可能还有其他一些缺陷。
无论如何这里是示例类:
public class MorseTranslator {
private char[] alpha = { '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
' ' };//Changed this to char to save some memory and to help my methods :3
private String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",
"--..", ".----", "..---", "...--", "....-", ".....", "-....",
"--...", "---..", "----.", "-----", "|" };
private String toMorse(String text) {
char[] characters = text.toUpperCase().toCharArray();
StringBuilder morseString = new StringBuilder();
for (char c : characters) {
for (int i = 0; i < alpha.length; i++) {
if (alpha[i] == c) {
morseString.append(morse[i]+" ");
break;
}
}
}
return morseString.toString();
}
private String fromMorse(String text){
String[] strings = text.split(" ");
StringBuilder translated = new StringBuilder();
for(String s : strings){
for(int i=0; i<morse.length; i++){
if(morse[i].equals(s)){
translated.append(alpha[i]);
break;
}
}
}
return translated.toString();
}
public MorseTranslator(){
String input = "Hello there";
String morse = toMorse(input);
String translated = fromMorse(morse);
System.out.println("Input: "+input);
System.out.println("Morse: "+morse);
System.out.println("Back From morse: "+translated);
}
}
上面的代码演示了一种可行的方法: - ) 希望这有所帮助。
答案 3 :(得分:0)
由于你不会说 完全不起作用,我不得不猜测。也许问题是String.equals()
方法区分大小写,即&#34; X&#34;和&#34; x&#34;不一样。但是,您似乎混合大写字母和非大写字母,因此可能是问题所在。将equals
替换为equalsIgnoreCase
时,您的方法是否有效?
更多指示:
首先,不要在字符串中使用字符串。您只需使用可以容纳单个字符的char
。
其次,不要使用两个数组来存储一对对象。维护两个阵列很麻烦且容易出错。如果你想使用一个数组,那么定义一个包含字符和莫尔斯代码字符串的类,并定义一个数组:
static class MorseMapping {
char character;
String morseCode;
MorseMapping(char ch, String mc) {
character = ch;
morseCode = mc;
}
}
static MorseMapping[] mappings = new MorseMapping[] {
new MorseMapping('A', '.-'),
\\ etc.
}
第三,我个人不喜欢使用一张地图,因为从概念上讲,你不想在一个方向上进行映射,而是在两者中进行映射。相反,使用两个地图,一个从字符到莫尔斯代码,一个从莫尔斯代码到哈希地图。您可以按如下方式初始化它们:
static Map<String,Character> charToMorseMap;
static Map<Character,String> morseToCharMap;
private static void addPair(String morse, char ch) {
charToMorseMap.put(ch, morse);
morseToCharMap.put(morse, ch);
}
static {
charToMorseMap = new HashMap<>();
MorseToCharMap = new HashMap<>();
// add all the characters
}
或者,您可以使用各种开源类库中的BiMap
实现,例如Google Guava。
答案 4 :(得分:0)
在类中封装莫尔斯 - 英语对,类似这样:
public class EnglishMorseLetter {
private String englishLetter;
private String morseLetter;
public MorseLetter(String english, String morse){
this.englishLetter = english;
this.morseLetter = morse;
}
public String getEnglishLetter() {
return englishLetter;
}
public void setEnglishLetter(String englishLetter) {
this.englishLetter = englishLetter;
}
public String getMorseLetter() {
return morseLetter;
}
public void setMorseLetter(String morseLetter) {
this.morseLetter = morseLetter;
}
}
使用静态类中的预定义值封装字典,如下所示:
public class Dictionary {
private static List<MorseLetter> dictionary = new ArrayList<MorseLetter>();
static {
dictionary.add(new MorseLetter("", ""));
dictionary.add(new MorseLetter("A", ".-"));
dictionary.add(new MorseLetter("B", "-..."));
dictionary.add(new MorseLetter("C", "-.-."));
dictionary.add(new MorseLetter("D", "-.."));
dictionary.add(new MorseLetter("E", "."));
dictionary.add(new MorseLetter("F", "..-."));
dictionary.add(new MorseLetter("G", "--."));
dictionary.add(new MorseLetter("H", "...."));
dictionary.add(new MorseLetter("I", ".."));
dictionary.add(new MorseLetter("J", ".---"));
dictionary.add(new MorseLetter("K", "-.-"));
dictionary.add(new MorseLetter("L", ".-.."));
dictionary.add(new MorseLetter("M", "--"));
dictionary.add(new MorseLetter("N", "-."));
dictionary.add(new MorseLetter("O", "---"));
dictionary.add(new MorseLetter("P", ".--."));
dictionary.add(new MorseLetter("Q", "--.-"));
dictionary.add(new MorseLetter("R", ".-."));
dictionary.add(new MorseLetter("S", "..."));
dictionary.add(new MorseLetter("T", "-"));
dictionary.add(new MorseLetter("U", "..-"));
dictionary.add(new MorseLetter("V", "...-"));
dictionary.add(new MorseLetter("W", ".--"));
dictionary.add(new MorseLetter("X", "-..-"));
dictionary.add(new MorseLetter("y", "-.--"));
dictionary.add(new MorseLetter("z", "--.."));
dictionary.add(new MorseLetter("1", ".----"));
dictionary.add(new MorseLetter("2", "..---"));
dictionary.add(new MorseLetter("3", "...--"));
dictionary.add(new MorseLetter("4", "....-"));
dictionary.add(new MorseLetter("5", "....."));
dictionary.add(new MorseLetter("6", "-...."));
dictionary.add(new MorseLetter("7", "--..."));
dictionary.add(new MorseLetter("8", "---.."));
dictionary.add(new MorseLetter("9", "----."));
dictionary.add(new MorseLetter("0", "-----"));
dictionary.add(new MorseLetter(" ", "|"));
}
public static MorseLetter english2Morse(MorseLetter letter){
for(MorseLetter tempLetter : dictionary){
if(letter.getEnglishLetter().equalsIgnoreCase(tempLetter.getEnglishLetter())){
letter.setMorseLetter(tempLetter.getMorseLetter());
break;
}
}
return letter;
}
public static MorseLetter morse2English(MorseLetter letter){
for(MorseLetter tempLetter : dictionary){
if(letter.getMorseLetter().equalsIgnoreCase(tempLetter.getMorseLetter())){
letter.setEnglishLetter(tempLetter.getEnglishLetter());
break;
}
}
return letter;
}
}
对于MCVE,我已经使用List
完成了它,但为了提高效率,我建议您使用两个不同的排序地图,包括英语 - 莫尔斯对和莫尔斯 - 英语对。 强>
还将类封装在类中:
public class EnglishMorseWord{
private List<EnglishMorseLetter> word;
public String getEnglishWord(){
//...you combine your word property into english word
}
public void setEnglishWord(String englishWord){
//...you parse your word and fill word property by using Dictionary
}
public String getMorseWord(){
//...you combine your word property into morse word
}
}
答案 5 :(得分:0)
另一个好方法是使用enum
public enum MorseCode {
A('A', ".-"),
B('B', "-..."),
C('C', "-.-."),
// ...
D8('8', "---.."),
D9('9', "----."),
D0('0', "-----");
private static final Map<String, MorseCode> FromCode = new HashMap<>(MorseCode.values().length);
private static final Map<Character, MorseCode> FromChar = new HashMap<>(MorseCode.values().length);
static {
for (MorseCode value : values()) {
FromCode.put(value.toCode(), value);
FromChar.put(value.toChar(), value);
}
}
public static MorseCode fromCode(String code) {
return FromCode.get(code);
}
public static MorseCode fromChar(Character character) {
return FromChar.get(character);
}
private final String Code;
private final Character Character;
private MorseCode(Character character, String code) {
Code = code;
Character = character;
}
public char toChar() {
return Character;
}
public String toCode() {
return Code;
}
}