Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法优化这个方法?_Java_Methods_Refactoring - Fatal编程技术网

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
),对吗?你不在乎其他专栏。