我有2个阵列.我想将第一个数组的索引转换为第二个数组.有没有比我下面更好的方法呢?
Array array1[9];
Array array2[3][3];
// Index is the index of the 1D array
public Point convert1Dto2D(int index)
{
        Point p = new Point();
        switch (index) {
            case 0:
                p.x = 0;
                p.y = 0;
                break;
            case 1:
                p.x = 0;
                p.y = 1;
                break;
            case 2:
                p.x = 0;
                p.y = 2;
                break;
            case 3:
                p.x = 1;
                p.y = 0;
                break;
            case 4:
                p.x = 1;
                p.y = 1;
                break;
            case 5:
                p.x = 1;
                p.y = 2;
                break;
            case 6:
                p.x = 2;
                p.y = 0;
                break;
            case 7:
                p.x = 2;
                p.y = 1;
                break;
            case 8:
                p.x = 2;
                p.y = 2;
                break;
        }
return p;
}
Sap*_*pph 36
p.x = index / 3;
p.y = index % 3;
你可以使用模数和整数除法进行数学运算,假设你的第二个数组是3x3数组,下面将做.
p.y = index % 3;
p.x = index / 3;