java,Arraylist,摩尔斯电码

时间:2018-04-05 16:33:49

标签: java arraylist

我有责任在java中编写一个morse代码转换器。我输入单词并将它们转换为莫尔斯代码。

我不应该使用HashMapHashSet。我可以使用ArrayList。我写了但是我有错误。你能帮我吗,请

错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.IndexOutOfBoundsException: Index: 65, Size: 0
    at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:685)
    at java.base/java.util.ArrayList.add(ArrayList.java:494)
    at com.company.Main.<clinit>(Main.java:25)

代码:

import java.util.ArrayList;
import java.util.List;

public class Main{

    final static String[][] code = {
            {"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", "--..   "}, {"0", "-----  "}, {"1", ".----  "},
            {"2", "..---  "}, {"3", "...--  "}, {"4", "....-  "}, {"5", ".....  "},
            {"6", "-....  "}, {"7", "--...  "}, {"8", "---..  "}, {"9", "----.  "},
            {"'", ".----. "}, {":", "---... "}, {",", "--..-- "}, {"-", "-....- "},
            {"(", "-.--.- "}, {".", ".-.-.- "}, {"?", "..--.. "}, {";", "-.-.-. "},
            {"/", "-..-.  "}, {"-", "..--.- "}, {")", "---..  "}, {"=", "-...-  "},
            {"@", ".--.-. "}, {"\"", ".-..-."}, {"+", ".-.-.  "}, {" ", "/"}};
    final static List<String> ascii = new ArrayList<String>();
static {
    for (String[] pair : code){
        ascii.add(pair[0].charAt(0), pair[1].trim());
    }
}
    public static void main(String[] args){
        printMorse("   Hello     World!");
    }
    static void printMorse(String input) {
        System.out.printf("%s %n", input);

        input = input.trim().replaceAll("[ ]+", " ").toUpperCase();
        for (char c : input.toCharArray()) {
            String s = ascii.get(c);
            if (s != null){
                System.out.printf("%s ", s);
            }
        }
        System.out.println("\n");
    }

}

1 个答案:

答案 0 :(得分:2)

您想将ascii声明为Map<Character, String>,而不是List<String>。它就像一个字典,其中字符是键,字符串是莫尔斯值

final static Map<Character, String> ascii = new HashMap<>();
static {
    for (String[] pair : code){
        ascii.put(pair[0].charAt(0), pair[1].trim());
    }
}

更改后,您的代码会运行并输出

   Hello     World! 
.... . .-.. .-.. --- / .-   - --- .-. .-.. -.. 

编辑:没有地图

如果您不希望Map效率下降,但可以这样写,那么您就不需要ascii列表。 对于每个字符,循环遍历code数组并找到对

final static String[][] code = {...}

public static void main(String[] args){
    printMorse("   Hello     World!");
}

static void printMorse(String input) {
    System.out.printf("%s %n", input);

    input = input.trim().replaceAll("[ ]+", " ").toUpperCase();

    for (char c : input.toCharArray()) {
        // Find the pair for the character
        for(String[] pair : code) {
            if(pair[0].equals(String.valueOf(c))) {
                System.out.printf("%s ", pair[1]);
            }
        }
    }

    System.out.println("\n");
}