Java 有没有办法优化这个方法?
我有这个代码,基本上检查是否有Java 有没有办法优化这个方法?,java,methods,refactoring,Java,Methods,Refactoring,我有这个代码,基本上检查是否有null。我使用了SonarQube,我必须尽可能多地优化,我觉得还可以,但我必须尝试。有什么想法吗 在下面的代码中,每个fila都有字符串的简单记录 public boolean isColumnNull(DbfReader reader, int[] pos) { Object[] fila = null; boolean isNull = false; int cont = 0; while (cont < pos.l
null
。我使用了SonarQube,我必须尽可能多地优化,我觉得还可以,但我必须尝试。有什么想法吗
在下面的代码中,每个fila
都有字符串的简单记录
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;
}
public boolean isColumnNull(DbfReader阅读器,int[]pos){
Object[]fila=null;
布尔值isNull=false;
int cont=0;
同时(续<位置长度){
while((fila=reader.nextRecord())!=null){
对于(int j=0;j
内部for循环可替换为
if ((pos[0] < fila.length && fila[pos[0]] == null) ||
(pos[1] < fila.length && fila[pos[1]] == null)) {
isNull = true;
break;
}
if((pos[0]
正如Andy Turner所建议的那样,应该检查pos[0]和pos[1]是否大于等于0,这应该在while循环开始之前执行一次。当然,您可以中断(或返回),因为isNull
不会再次变为false。@Andy Turner是的,我将isNull作为每条记录读取,但它适用于整个方法。谢谢。(并检查pos[n]>=0
)该代码看起来是错误的。它将读取(至少)pos.length
记录数,但仅使用pos[0]
和pos[1]
,而不考虑数组长度?嗯?!?--另外,如果nextRecord()
返回null,这可能意味着数据结束,因此外部循环将重试,并且将永远循环。因此pos
保留记录中的位置以检查null字段,我就明白了这一点。但是为什么的外循环(cont
?@Turing85.pos.length
始终为2,值会发生变化while(cont
因为有更多的列,但我总是对数组中的2感兴趣,例如culd是3和7,但是您只想检查pos
数组中指示的列中是否有任何记录null
(例如列3
和7
),对吗?你不在乎其他专栏。