【发布时间】: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