比较长字符串数组的最有效方法

时间:2014-04-16 12:06:26

标签: android

我正在使用语音识别器从用户那里获取语音输入,它返回一个包含5个字符串的数组,我将其传递给此方法

    public int analyzeTag(ArrayList<String> voiceResults,Editor editor, Context context){
            for (String match : voiceResults) {
                Log.d(TAG, match);
                if (match.equalsIgnoreCase(context.getResources().getString(R.string.first_tag))){
                    editor.append(context.getResources().getString(R.string.first_tag));
                    return 1;
                }
                else if (match.equalsIgnoreCase(context.getResources().getString(R.string.second_tag))){
                    editor.append(context.getResources().getString(R.string.second_tag));
                    return 1;
                }
                //etc....(huge list of tags)
                //Some tags might also have acceptable variations, example:
                else if (match.equalsIgnoreCase("img") || match.equalsIgnoreCase("image")
                {
                     editor.append("img"); //the string to append is always taken from the first variation
                }

            }
            return 0;
        }

此方法将结果与标签列表进行比较,标签列表相当大,有数百个标签,所以我想找到最有效的方法来执行此操作。

我需要帮助:

1 。我比较结果的方式最有效吗?有没有更好的办法? (从用户体验的角度来看,我不希望用户等待很长时间才能得到结果)。 语音输入将是我的应用程序的重要部分,因此这种方法将经常被调用

2 。我有很长的标签列表,显然if(),elseIf()路线会非常重复,有没有办法迭代这个?考虑到某些标签可能具有变化(甚至超过1)并且变体1(“img”)对于每个人都是相同的事实,但是其他变体将是区域/语言敏感的示例:英语用户的“图像” immagini“为意大利用户等 附加到编辑器的文本将始终取自第一个变体

1 个答案:

答案 0 :(得分:2)

如何将标记放入字符串数组然后迭代数组?

  String[] tags = context.getResources().getStringArray(R.array.tags);
    for (String match : voiceResults) {
        for (int index = 0; index < tags.length; index++ ) {            
            if (match.equalsIgnoreCase(tags[index]) {
                editor.append(tags[index]);
            }
        }
    }

这是doc on StringArray