我正在尝试为电话拨号盘创建一个号码关联映射,如下所示:
private Integer[][] dialpad = { {1 , 2 , 3},
{4 , 5 , 6},
{7 , 8 , 9},
{null, 0 , null} };
现在,根据:的规则关联:
1 should 2,4,5
2 should be 1,3,4,5,6
3 should be 2,5,6
and so on............
我已经编写了处理此问题的代码:
public void createDialAssociation()
{
int nosCols = dialpad[0].length;
int nosRows = dialpad.length;
//int rowFrom, rowTo, colFrom, colTo;
for(int row=0; row<nosRows; row++)
{
for(int col=0; col<nosCols; col++)
{
Integer currentElement = dialpad[row][col];
elementAssociation.put( currentElement, new ArrayList<Integer>());
int rowFrom = (row-1)< 0 ? 0 : (row-1);
int rowTo = (row+1) <= (nosRows-1)? row+1: nosRows-1;
int colFrom = (col-1)<0 ? 0 : (col-1);
int colTo = (col+1) <= (nosCols-1) ? col+1 : nosCols-1;
LOG.info("row,col,element = " + row + "," +col+ ","+ currentElement);
LOG.info("rowFrom = " + rowFrom);
LOG.info("rowTo = " + rowTo);
LOG.info("colFrom = " + colFrom);
LOG.info("colTo = " + colTo);
LOG.info("---------------------------------------------------- " );
for(int currentRowIndex=rowFrom; currentRowIndex==rowTo; currentRowIndex++)
{
LOG.info("1..............");
for(int currentColIndex = colFrom; currentColIndex == colTo ; currentColIndex++)
{
LOG.info("2..............");
if ( currentRowIndex == row || currentColIndex == col )
continue;
if( dialpad[currentRowIndex][currentColIndex] != null)
{
elementAssociation.get(currentElement).add(dialpad[currentRowIndex][currentColIndex]);
}
}
}
}
}
我的问题是,代码不在循环内:
for(int currentRowIndex=rowFrom; currentRowIndex==rowTo; currentRowIndex++)
因此,我看不到
LOG.info("1.......);
or LOG.info("2........);
我期望得到任何见解/帮助吗?
答案 0 :(得分:2)
for(int currentRowIndex=rowFrom; currentRowIndex==rowTo; currentRowIndex++){}
将currentRowIndex
初始化为rowFrom
的值。
在currentRowIndex==rowTo
期间,它运行循环的内容,并在每次执行循环后运行currentRowIndex++
。
因此,循环恰好在rowFrom==rowTo
时最多进入一次。
您可能要继续循环直到currentRowIndex==rowTo
,甚至可能包括这种情况。这意味着您必须在rowFrom
仍低于rowTo
的情况下运行继续循环,因此必须编写rowFrom<rowTo
或rowFrom<=rowTo
。
内循环也是如此。