我有几个以不同组合出现的二维数组,其中一个定义了中心,另外八个逻辑(而非物理上)放置在左侧,右侧,顶部,右上角等等。 '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也可能会产生一些影响。 –