我有大約10多個類,每個類都有一個LUMP_INDEX和SIZE靜態常量。 我想要這些類中的每一個的數組,其中使用這兩個常量來計算數組的大小。 目前我有一個函數爲每個類創建陣列,沿着線的東西:重複此java代碼複製
private Plane[] readPlanes()
{
int count = header.lumps[Plane.LUMP_INDEX].filelen/Plane.SIZE;
Plane[] planes = new Plane[count];
for(int i = 0; i < count; i++)
planes[i] = new Plane();
return planes;
}
private Node[] readNodes()
{
int count = header.lumps[Node.LUMP_INDEX].filelen/Node.SIZE;
Node[] nodes = new Node[count];
for(int i = 0; i < count; i++)
nodes[i] = new Node();
return nodes;
}
private Leaf[] readLeaves()
{
int count = header.lumps[Leaf.LUMP_INDEX].filelen/Leaf.SIZE;
Leaf[] leaves = new Leaf[count];
for(int i = 0; i < count; i++)
leaves[i] = new Leaf();
return leaves;
}
等 有這些功能中的10,唯一的差別是類的類型,從而你可以看到,有很多重複。
有沒有人有任何想法如何避免這種重複? 謝謝。 (我之前問過類似的問題,但我猜我的方式有點偏離)
你在void方法中有return語句嗎? – 2011-03-26 19:21:24
這些方法中的'header'對象是什麼? – 2011-03-26 19:36:05
爲什麼不使用ArrayList? – bancer 2011-03-26 21:25:08