【问题标题】:Can I combine a bunch of if statements into an object?我可以将一堆 if 语句组合成一个对象吗?
【发布时间】:2015-01-03 19:31:18
【问题描述】:

我有一个包含数千个单行句子的 ArrayList,我想搜索大约 50 个关键词。如果它包含这些关键词之一,我希望它将对应于该关键词的双精度词添加到数组中。一旦检查了所有数组是否匹配,我想总结数组的总数。

目前我有类似的东西,除了数组列表和双变量列表要大得多:

import java.util.ArrayList;

public class searchArrayList {

public static void main(String[] args){


String subject2 = "The color of the ball is blue."; 
String subject3 = "The building is white.";
String subject4 = "Red is my favorite color.";  
String subject5 = "Black and blue are the team colors";



    ArrayList<Double> arrlist = new ArrayList<Double>();
    ArrayList<Double> difflist = new ArrayList<Double>();

    double Black = 10.1;
    double Blue = 1.6;
    double Red = 11.4;
    double White = 4.3;

    String[] subjectArray = {subject2,subject3,subject4,subject5};

    for (String subjects: subjectArray)
    {
        if (subjects.toUpperCase().contains("BLUE"));
        System.out.println(Blue+ "Blue ");
        arrlist.add(Blue);
        if (subjects.toUpperCase().contains("BLACK"));
        System.out.println(Black+" Black");
        arrlist.add(Black);
        if (subjects.toUpperCase().contains("WHITE"));
        System.out.println(White+" White");
        arrlist.add(White);
        if (subjects.toUpperCase().contains("RED"));
        System.out.println(Red+" Red");
        arrlist.add(Red);
    }
    System.out.println(sum(arrlist));
}

public static double sum(ArrayList<Double>arrlist)
{
  double result = 0;
  for (double number:arrlist)
  result += number;
  return result;
    }


   }

这可以满足我的需要,但我想知道是否有更好或更直观的方法来做到这一点?我可以将 if 语句组合到单个对象/类,然后调用该类来搜索数组吗?使用switch语句会更好吗?我尝试使用正则表达式进行搜索,但 .contains 似乎也能正常工作。

【问题讨论】:

  • 您无法使用 switch-case 实现相同的效果,您的解决方案似乎还可以。但是,可以有更好的......
  • 首先,最好将所有要搜索的单词也移动到数组中。然后要获取每个单词的计数,您可以使用 double[][] 数组并在那里保存相关值。或哈希图。
  • 同意奥雷斯特。如果您也将搜索词放在一个数组中,您可以在没有所有 if 语句的情况下执行此操作。
  • 好的,那么创建一个包含所有关键词的 ArrayList?如何通过 KeywordArrayList 搜索 SubjectArrayList 的每个元素?
  • 这是我需要的我不这么认为,每个if语句后面都有一个分号(;),所以@后面的所有行987654323@s 将被执行。

标签: java arrays arraylist


【解决方案1】:

只需使用包含关键字-double 关联的数组(或集合),然后遍历该数组即可:

public class Keyword {
    private String value;
    private double points;

    ...
}

private Keyword[] keywords = new Keyword[] {
    new Keyword("BLUE", 1.6),
    new Keyword("BLACK", 10.1),
    ...
}

for (String subject : subjectArray) {
    for (Keyword keyword : keywords) {
        if (subject.toUpperCase().contains(keyword.getValue())) {
            System.out.println(keyword.getPoints() + " " + keyword.getValue());
            arrlist.add(keyword.getPoints());
        }
    }
}

【讨论】:

  • 非常好,我试试看。
【解决方案2】:

我没有足够的声誉来发表评论,但这不比将字符串转换为大写更有效吗?

org.apache.commons.lang3.StringUtils.containsIgnoreCase(String, String)

库文档:http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2021-08-01
    • 2011-05-25
    • 1970-01-01
    • 2020-06-24
    相关资源
    最近更新 更多