我有幾個以不同組合出現的二維數組,其中一個定義了中心,另外八個邏輯(而非物理上)放置在左側,右側,頂部,右上角等等。 'new array'的訪問方式如下: 如果x和y索引位於中心數組的邊界內,則訪問中心數組。如果索引超出邊界(或者是高於或者高於< 0),則訪問適當的相鄰數組(例如,對於x < 0和y < 0,左上角的數組被訪問)。Java在邏輯上連接數組
在此刻我正在手動解決數組索引的重新映射。下面是一個例子(沒有必要了解情況,我只是想表明所描述的過程中的結構):
public float noiseValueForCorner(int x, int y, int colorRight, int colorLeft, int colorTop,
int colorBottom, int tileLeftUp, int tileRightUp, int tileLeftLower, int tileRightLower)
{
// Out of bounds
if (x < -K2 || y < -K2 || x >= 4*K || y >= 4*K)
return -1.0f;
if (y >= 0 && y < K2)
{
// corner itself
if (x >= 0 && x < K2)
return CornerNoise.noise[x][y];
// left color
else if (x < 0 && colorLeft >= 0)
return ColorNoise[colorLeft].noise[K2-y-1][N2K+x];
// right color
else if (x >= K2 && colorRight >= 0)
return ColorNoise[colorRight].noise[K2-y-1][x-K2];
}
else if (y < 0)
{
// top color
if (x >= 0 && x < K2 && colorTop >= 0)
return ColorNoise[colorTop].noise[x][N2K+y];
// top left tile
else if (x < 0 && tileLeftUp >= 0)
return TileNoise[tileLeftUp].noise[N2K+x][N2K+y];
// top right tile
else if (x >= K2 && tileRightUp >= 0)
return TileNoise[tileRightUp].noise[x-K2][N2K+y];
}
else //if (y >= K2)
{
// bottom color
if (x >= 0 && x < K2 && colorBottom >= 0)
return ColorNoise[colorBottom].noise[x][y-K2];
// bottom left tile
else if (x < 0 && tileLeftLower >= 0)
return TileNoise[tileLeftLower].noise[N2K+x][y-K2];
// bottom right tile
else if (x >= K2 && tileRightLower >= 0)
return TileNoise[tileRightLower].noise[x-K2][y-K2];
}
return 2.0f;
}
的問題是,由於這種方法的許多人呼籲我的整個程序是相當慢。我想知道是否有更好的方法來做到這一點。我想到了一些有效的方法來將索引轉換爲單個數組的內存地址,以獲得一個新數組(因爲數組訪問完成得相當快)。
陣列內,將其卸載到一個線程的幫助? –
陣列或其放置位置更改的頻率以及您訪問「放置配置」的頻率? – Robert
除了手動編碼所有情況下繁瑣的手動編碼之外,所顯示的代碼不一定很慢。使用分析器查找使用頻率和瓶頸。 FindBugs也可能會產生一些影響。 –