这段代码:
private int GetColumnToPopulate(int columnBase, int offset) {
int duckbillColNum = 0;
switch (columnBase) {
case 0:
duckbillColNum = 1;
break;
case 1:
duckbillColNum = 5;
break;
case 2:
duckbillColNum = 9;
break;
case 3:
duckbillColNum = 13;
break;
case 4:
duckbillColNum = 17;
break;
case 5:
duckbillColNum = 21;
break;
case 6:
duckbillColNum = 25;
break;
case 7:
duckbillColNum = 29;
break;
}
duckbillColNum += offset;
return duckbillColNum;
}
...很容易理解,但冗长而笨拙。我想精简它。我的伪代码想法是这样的:
int[] colBases
{
0..7
}
int[] offsets
{
1,5,9,13,17,21,25,29
}
int idx = colBases.IndexOf(columnBase);
duckbillColNum = offsets[idx];
...或者也许是一个多维 int 数组?无论如何,“IndexOf”业务当然不会编译[ile,ute]。