Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android IfElse或regex_Android_Regex_If Statement - Fatal编程技术网

Android IfElse或regex

Android IfElse或regex,android,regex,if-statement,Android,Regex,If Statement,当你有很长的时间的时候,哪一个是最好的方法 if (text.contains("text")) { // do the thing } else if (text.contains("foo")) { // do the thing } else if (text.contains("bar")) {

当你有很长的时间的时候,哪一个是最好的方法

        if (text.contains("text"))
        {
            // do the thing
        }
        else if (text.contains("foo"))
        {
            // do the thing
        }
        else if (text.contains("bar"))
        {
            // do the thing
        }else ...

或许

        Pattern pattern = Pattern.compile("(text)|(foo)|(bar)|...");
        Matcher matcher = pattern.matcher(text);
        if(matcher.find())
        {
            // do the thing
        }
我的意思是,只有当你必须检查很多的时候。谢谢

String[]存储={
String[] storage = {
    "text",
    "foo",
    "bar",
    "more text"
};

for(int i=0; i < storage.length(); i++){
    //Do Something
}
“文本”, “福”, “酒吧”, “更多文本” }; 对于(int i=0;i
这有帮助吗?

字符串[]存储={ “文本”, “福”, “酒吧”, “更多文本” }; 对于(int i=0;i
这有帮助吗?

我个人会使用一个集合,因为我认为它更容易阅读,而且
包含的
在O(1)中会更有效:


我个人会使用集合,因为我认为它更容易阅读,并且
包含的
在O(1)中会更有效:


通常长
If
else
语句替换为
case
语句,但这并不总是可能的。如果我向何处推荐,我会选择第二个选项,选项1将给您一组执行相同操作的
If-else If-else
语句,而对于第三种情况,正则表达式往往增长得非常快


同样,根据
alot
的多少,最终最好将所有字符串放入一个数据结构中,并对其进行迭代以查看元素是否在其中。

如果
else
语句替换为
case
语句,则通常为long
,但这并不总是可能的。如果我向何处推荐,我会选择第二个选项,选项1将给您一组执行相同操作的
If-else If-else
语句,而对于第三种情况,正则表达式往往增长得非常快


同样,根据
alot
的多少,最终最好将所有字符串放入一个数据结构中,并对其进行迭代以查看元素是否在其中。

我认为它实际上是O(logn),因为集合在内部存储为二叉树。不过,哈希表的摊销效率为O(1)。从javadoc中可以看出:“这个类为基本操作(添加、删除、包含和大小)提供了恒定的时间性能,假设哈希函数将元素正确地分散在各个存储桶中”——这应该是字符串的情况。我的坏消息。我把它们与C++集混淆,这些集合通常是用二叉搜索树来实现的,但这不是他想要的吗?如果
text
“foo1”
text.contains(“foo”)
将返回true,但是
关键字.contains(text)
将是false,因为关键字只包含
“foo”
?@Jave你是对的,我的建议并不等同于他所做的。编辑了我的答案。我认为它实际上是O(logn),因为集合在内部存储为二叉树。不过,哈希表的摊销效率为O(1)。从javadoc中可以看出:“这个类为基本操作(添加、删除、包含和大小)提供了恒定的时间性能,假设哈希函数将元素正确地分散在各个存储桶中”——这应该是字符串的情况。我的坏消息。我把它们与C++集混淆,这些集合通常是用二叉搜索树来实现的,但这不是他想要的吗?如果
text
“foo1”
text.contains(“foo”)
将返回true,但是
关键字.contains(text)
将是false,因为关键字只包含
“foo”
?@Jave你是对的,我的建议并不等同于他所做的。编辑了我的答案。
String[] storage = {
    "text",
    "foo",
    "bar",
    "more text"
};

for(int i=0; i < storage.length(); i++){
    //Do Something
}
Set<String> keywords = new HashSet<String>();
keywords.add("text");
keywords.add("foo");
keywords.add("bar");

if(keywords.contains(text)) {
    //do your thing
}
Set<String> keywords = new HashSet<String>(Arrays.asList("text", "foo", "bar"));

if(keywords.contains(text)) {
    //do your thing
}
for (String key : keywords) {
    if (text.contains(key)) {
        //do your stuff
        break;
    }
}