Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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
Java 寻找帮助创建更有效的方法做大量的检查_Java - Fatal编程技术网

Java 寻找帮助创建更有效的方法做大量的检查

Java 寻找帮助创建更有效的方法做大量的检查,java,Java,注意:不是的副本,因为我正在进行一些检查,以确定某个内容是否继承了其他内容 这是一种更好、更有效的方法: 正如你所看到的,我输入了2个字符串,然后在列表中检查它们,就好像current=3,那么检查1、2和3就会返回true 注意:这些值one、two、three只是我使用的示例的占位符,它们之间没有关系,只是它们具有不同的优先级 public boolean checker(String current, String check) { if (check.equals("one"

注意:不是的副本,因为我正在进行一些检查,以确定某个内容是否继承了其他内容

这是一种更好、更有效的方法:

正如你所看到的,我输入了2个字符串,然后在列表中检查它们,就好像current=3,那么检查1、2和3就会返回true

注意:这些值one、two、three只是我使用的示例的占位符,它们之间没有关系,只是它们具有不同的优先级

public boolean checker(String current, String check) {


    if (check.equals("one")) {
        if (current.equals("one") || current.equals("two")
                || current.equals("three")) {
            return true;
        }
    }
    if (check.equals("two")) {
        if (current.equals("two") || current.equals("three")) {
            return true;
        }
    }
    if (check.equals("three")) {
        if (current.equals("three")) {
            return true;
        }
    }
    return false;

}

这里有几点建议

正如Frisch在注释中提到的,使用.equals而不是==进行字符串比较

使用开关/案例

switch (check) {
    case "one":
        if (current.equals("one")) return true;
    case "two":
        if (current.equals("two")) return true;
    case "three":
        if (current.equals("three")) return true;
}
除此之外,似乎没有什么事可做。

两件事

不要使用相等来检查字符串。使用.equals方法。您可以取消字符串文字。像这样。即使使用空值,将其从字符串文本中取消也是安全的,这通常是一件好事

if ("one".equals(check))
您可以利用Java的短路运算符&&和||

可以成为

if ("one".equals(check) && ("one".equals(current) || "two".equals(current) || "three".equals(current))) {
    return true;
}
将从左到右对其进行评估。由于one.equalscheck位于最左边,并且与语句的其余部分&&,因此如果one.equalscheck不为true,Java将退出条件检查,并且不会对语句的其余部分求值

由于您只是返回true或false,您还可以进一步使用De Morgan's laws的_定律将所有if语句简化为一个单独的if语句。通常,以最自然的方式声明布尔if语句,然后通过应用保持逻辑if语句不变的转换来简化它

一个很好的例子是,从下面的链接中窃取

在主方法程序体的上下文中,假设定义了以下数据:

int score, min = 0, max = 20;
boolean bad, good;
进一步假设一个值被分配给分数,可能来自键盘输入,我想用一个布尔表达式测试分数是否为有效数字。好的分数在封闭范围[0..20]内,包括0和20

good = (score >= min && score <= max);

I would like to get the score from the keyboard in a do while loop, so that I can validate the entry. The logic in my control structure is to demand another entry for the score while the entry is bad. I have a definition of a good entry, and I will use definitions of operators and De Morgan's Law to help me write an expression that represents a bad entry.

good = (score >= min && score <= max); // original definition of good from the logic of my task 
good = !(score < min) && !(score > max); // by definition, >= means ! < , <= means ! >
good = !(score < min || score > max); // by De Morgan's' Law
bad = !good ; // bad is not good
bad = !!(score < min || score > max); // substituting for good
bad = score < min || score > max; // double negation is dropped

我想告诉你一些最新情况。 1.我们只能在基本数据类型上应用开关情况,而不能在对象上应用开关情况。由于字符串是对象,我们不能在case/switch语句中使用字符串

在这种情况下,我建议您使用枚举/映射

请查找下面的示例程序,我是如何使用maps实现的

 public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);

        String current = "one";
        String check = "one";
        if(map.get(check)<=map.get(current)){
            System.out.println("Our condition is success");
        }
    }
这比多重比较更好


--Santhosh

不使用==测试字符串是否相等。您的逻辑与if check.equalscurrent有何不同?您可能希望使用更能代表实际用例的数据。这些字符串1、2和3是唯一可能的字符串集吗?如果是这样,您可以使用枚举来表示它们,将If构造替换为switch cases等等。TBF,在这种上下文中,嵌套的If比&&更具可读性。方法名上有拼写错误-它等于。我强烈不同意你关于清晰性的断言。但这不是一个风格指南,而是一个关于如何更好、更高效的问题。我不确定编译器是否足够聪明,能够将内部块提取成一个大的布尔值,以便您创建这个答案中给出的短路操作类型,但是如果您有一组很长的条件要检查,那么确保第一个被检查的是一个简单的条件是一个很大的优先级。它实际上是简单性/返回false和停止布尔链的机会的混合,但这是无关的。是的,但几乎所有的应用程序都支持Android或更喜欢applets Java 8这与check有什么不同。equalscurrent?它干净多了我想这是你的意图。它的作用与您的整个函数完全相同。
 public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);

        String current = "one";
        String check = "one";
        if(map.get(check)<=map.get(current)){
            System.out.println("Our condition is success");
        }
    }