这个对象就是它的全部内容:
public class Unit {
private String label;
NumberRow numberRow;
Unit(String label){
this.label=label;
numberRow = new NumberRow();
}
....
}
它包含一个标签名称和一个双精度数组(即 NumberRow())另一个名为 UnitRow 的类是这些单元的数组:
public class UnitRow {
Unit[] unitArray;
private int elementsInArray;
Scanner in;
UnitRow(){
unitArray = new Unit[Dataset.numberOfRecords];
elementsInArray = 0;
}
......
}
然而我要介绍另一个类:Leaf。它包含一个单元并实现集群接口:
public class Leaf implements Cluster {
private Unit unit;
Leaf(Unit unit){
this.unit = unit;
}
.......
public UnitRow getUnits() {
UnitRow unitRow = new UnitRow();
unitRow.addLabel(unit.getLabel());
for (int x = 0; x < Dataset.numberOfVariables; x++){
unitRow.addVar(unit.valueOnIndex(x));
}
return unitRow;
}
public boolean hasChildren() {
return false;
}
}
它包含一个 Unit,并且在函数 UnitRow 中创建一个新的 UnitRow,其中只有一个 Unit;即创建类时给出的单位。另一个名为 Node 的类(最后一个)也实现了集群接口:
public class Node implements Cluster{
private Cluster leftChild;
private Cluster rightChild;
Node(Cluster leftChild, Cluster rightChild){
this.leftChild = leftChild;
this.rightChild = rightChild;
}
public UnitRow getUnits() {
return leftChild.getUnits() + rightChild.getUnits();
}
}
一个节点有一个左孩子和一个右孩子。这样的孩子可以是叶子或另一个节点。现在我在 Node 中有一个函数,它为我提供了一个来自其下方所有叶子的单元数组。我要告诉 java: return leftChild.getUnits() + rightChild.getUnits(); 因此,例如,如果 leftChild 和 rightChild 都是叶子,则它们将返回的那些数组在该语句中相加。然而,这不是正确的方法。让 Node 中的函数 getUnits 以最有效的方式返回一个带有 Units 的数组的最有效方法是什么?