【问题标题】:checking if my array elements meet requirements检查我的数组元素是否满足要求
【发布时间】:2016-04-26 01:42:26
【问题描述】:

我需要创建一个方法来检查数组中的每个元素以查看它是真还是假,每个元素都包含一个化合物的多个值,例如质量、分子式、面积等,总共有 30 个化合物 (所以数组有30个元素)。我需要一个算法来询问质量 5 = true 是否。

我的属性类看起来像:

public void addProperty (Properties pro )
        {
        if (listSize >=listlength)
        {
        listlength = 2 * listlength;
        TheProperties [] newList = new TheProperties [listlength];
        System.arraycopy (proList, 0, newList, 0, proList.length);
        proList = newList;
        }
        //add new property object in the next position
        proList[listSize] = pro;
        listSize++;

        }

        public int getSize()
        {
        return listSize;
        }
        //returns properties at a paticular position in list numbered from 0
        public TheProperties getProperties (int pos)
        {
        return proList[pos];
        }
        }

使用 TheProperties 中的 getter/setter 后,我使用以下命令将所有信息放入数组中;

TheProperties tp = new properties();

string i = tp.getMass();
String y = tp.getArea();
//etc
theList.addProperty(tp);

然后我使用以下内容保存文件的输出;

    StringBuilder builder = new StringBuilder();

    for (int i=0; i<theList.getSize(); i++)
    {
        if(theList.getProperties(i).getFormatted() != null)
        {
            builder.append(theList.getProperties(i).getFormatted());
            builder.append("\n");
        }
    }           

    SaveFile sf = new SaveFile(this, builder.toString());

我只是不知道如何单独询问每个化合物是否达到值,读取文件并为每个保存的值设置一个值,然后我可以为要检查的要求,但如何实际检查每个化合物的元素是否符合要求?我正在尽我所能,我仍在努力学习我相当差的 Java 技能。

【问题讨论】:

  • 我建议的一件事是为每个参数定义一个数组,例如一个数组代表质量,一个数组代表面积等等,然后每个数组的第一个元素代表第一个对象。通过这种方式,您拥有更多控制权。如果您使用的是 Java 8,我建议您检查并行数组:mathbits.com/MathBits/Java/arrays/ParallelArrays.htm

标签: java arrays if-statement boolean


【解决方案1】:

不完全确定你在追求什么,我发现你的描述很难理解,但是如果你想看看质量是否小于 50,面积是否大于 5,一个简单的 if 语句,像这样,将做。

if (tp.getMass() < 50 && tp.getArea() > 5) {}

尽管您将再次实例化 tp 并确保已通过某种构造函数为其赋予了属性。

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点,这很难回答。

    您可以在创建时检查,甚至不将无效的添加到列表中。这意味着您只需循环一次。

    如果你只想将输出保存到文件中,不做任何其他事情,我建议你将读取和写入合二为一。

        Open up the read and the write file
        while(read from file){
            check value is ok
            write to file
        }
        close both files
    

    这样做的好处是:

    1. 你只循环一次,而不是三次,所以它更快
    2. 您无需将整个列表存储在内存中,因此您可以处理包含数千个元素的非常大的文件。

    【讨论】:

      【解决方案3】:

      如果需求发生变化,您可以编写使用Predicate&lt;T&gt; 的方法,这是专为这种情况设计的FunctionalInterface(Java 8 中引入了functionalInterfaces):

      // check each element of the list by custom condition (predicate)
      public static void checkProperties(TheList list, Predicate<TheProperties> criteria) {
          for (int i=0; i < list.getSize(); i++) {
              TheProperties tp = list.get(i);
              if (!criteria.apply(tp)) {
                  throw new IllegalArgumentException(
                        "TheProperty at index " + i + " does not meet the specified criteria");
              }
          }
      } 
      

      如果你想检查质量是否 5,你会写:

      checkProperties(theList, new Predicate<TheProperties> () {
      
          @Override 
          public boolean apply(TheProperties tp) {
              return tp.getMass() < 50 && tp.getArea() > 5;
          }
      
      }
      

      这可以通过使用 lambda 表达式来缩短:

      checkProperties(theList, (TheProperties tp) -> {
          return tp.getMass() < 50 && tp.getArea() > 5;
      });
      

      【讨论】:

      • 谢谢,这真的很有趣,我会研究一下。
      猜你喜欢
      • 2022-01-02
      • 2012-10-01
      • 2017-06-20
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多