将一维数组索引转换为二维数组索引 [英] Convert 1D array index to 2D array index
本文介绍了将一维数组索引转换为二维数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个数组.我想将第一个数组的索引转换为第二个.有没有比我下面的方法更好的方法?
I have 2 arrays. I want to convert the index of the first array to the second. Is there a better way to do it than what I have below?
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;
}
推荐答案
p.x = index / 3;
p.y = index % 3;
这篇关于将一维数组索引转换为二维数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文