有什么方法可以优化此方法?

时间:2018-12-26 20:31:27

标签: java methods refactoring

我有这段代码,基本上检查是否有fila。我使用了 SonarQube ,我必须尽我所能进行优化,我觉得还可以,但是我必须尝试。有什么想法吗?

在以下代码中,每个Strings都有简单的记录public boolean isColumnNull(DbfReader reader, int[] pos) { Object[] fila = null; boolean isNull = false; int cont = 0; while (cont < pos.length) { while ((fila = reader.nextRecord()) != null) { for (int j = 0; j < fila.length; j++) { if ((j == pos[0] || j == pos[1]) && fila[j] == null) { isNull = true; break; } } cont++; } } return isNull; }

+---------+-----+
| SubTribe|count|
+---------+-----+
|    Chill|   10|
|     Cool|   18|
|Adventure|   18|
|    Quirk|   13|
|  Mystery|   25|
|    Party|   18|
|Glamorous|   13|
+---------+-----+

1 个答案:

答案 0 :(得分:1)

The inner for loop can be replaced with

if ((pos[0] < fila.length && fila[pos[0]] == null) || 
    (pos[1] < fila.length && fila[pos[1]] == null)) {
    isNull = true;
    break;
}

As suggested by Andy Turner both pos[0] and pos[1] should be checked that they are >= 0, this should be done once before the while loop starts.