Java:如何遍历类型链接列表的数组并将它们附加到数组

标签 java arrays algorithm linked-list radix

对于算法课的家庭作业,我们必须编写一个程序来实现基数排序算法。我最终以一种循环的方式实现了它,并且它运行正常。但是,我的代码中有一部分看起来很糟糕,如果 else 阻塞在 for 循环中。我必须以正确的顺序从链表数组中检索项目,并将元素添加回整数数组。我和我的一个同学花了很长时间试图弄清楚如何将这个 block 放入 for 循环中,但就是想不出一个办法来做到这一点。这就是我的问题,我如何将数组中链表的对象放入不同的数组中。我为排序方法编写的代码如下:

private static Integer[] sort(Integer[] input, int place){
    //create an array of linked lists
    LinkedList<Integer>[] bucketsOut = new LinkedList[10];

    //initialize the linked lists
    for(int i=0; i < 10; i++){
        bucketsOut[i] = new LinkedList<Integer>();
    }

    int bucketPlacement = 0;
    //place every input into the correct bucket
    for(int i = 0; i < input.length; i++){
        bucketPlacement = getDigit(input[i].intValue(), place);
        bucketsOut[bucketPlacement].add(input[i]);
    }

    //Place the elements out of the linked lists into the correct place in input[]
    for(int i = 0; i < input.length; i++){ //for each input number
        if(bucketsOut[0].peekFirst() != null){  
            input[i] = bucketsOut[0].pollFirst().intValue();
        }else if(bucketsOut[1].peekFirst() != null){    
            input[i] = bucketsOut[1].pollFirst().intValue();
        }else if(bucketsOut[2].peekFirst() != null){    
            input[i] = bucketsOut[2].pollFirst().intValue();
        }else if(bucketsOut[3].peekFirst() != null){    
            input[i] = bucketsOut[3].pollFirst().intValue();
        }else if(bucketsOut[4].peekFirst() != null){    
            input[i] = bucketsOut[4].pollFirst().intValue();
        }else if(bucketsOut[5].peekFirst() != null){    
            input[i] = bucketsOut[5].pollFirst().intValue();
        }else if(bucketsOut[6].peekFirst() != null){    
            input[i] = bucketsOut[6].pollFirst().intValue();
        }else if(bucketsOut[7].peekFirst() != null){    
            input[i] = bucketsOut[7].pollFirst().intValue();
        }else if(bucketsOut[8].peekFirst() != null){    
            input[i] = bucketsOut[8].pollFirst().intValue();
        }else if(bucketsOut[9].peekFirst() != null){    
            input[i] = bucketsOut[9].pollFirst().intValue();
        }
    }
    //return sorted list for digit
    return input;
}

最佳答案

当你有一系列操作除了索引的改变之外完全相同时,这意味着你可以在for循环中完成:

第一次尝试

for (int j = 0; j < bucketsOut.length; j++ ) {
    // The part that is repeated again and again
    if (bucketsOut[j].peekFirst() != null) {
        input[i] = bucketsOut[j].pollFirst().intValue();
    }
}

但是等等!这将在所有桶中继续。您原来的 if 结构实际上意味着一旦您点击了正确的 if,您将不会查看任何其他 else

这可以通过在条件变为真时跳出循环来完成:

改进版

for (int j = 0; j < bucketsOut.length; j++ ) {
    if (bucketsOut[j].peekFirst() != null) {
        input[i] = bucketsOut[j].pollFirst().intValue();
        break; // Now the j loop will stop when we hit the first non-null.
    }
}

或者您可以使用增强型 for - 逻辑是相同的:

for ( LinkedList<Integer> bucket : bucketsOut ) {
    if (bucket.peekFirst() != null) {
        input[i] = bucket.pollFirst().intValue();
        break; 
    }
}

关于Java:如何遍历类型链接列表的数组并将它们附加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33903852/

相关文章:

Java:引用另一个类中的构造函数

javascript - "Remove"评估为 true 时的测试条件

java - 组合算法并行化

java - 将 Netty channel 传递到队列并稍后将其用于不同线程上的写入是否有效?

java - 在java中生成14-24之间的100个随机数并放入字节数组中

java - 如何在两个文本文件中查找相似的行,而不考虑它们出现的行号

使用像 printf 中的变量在 C 中创建一个 char 数组

javascript - 我如何检查这个物体的长度?

algorithm - 使用开放链和单独寻址检查成员资格

algorithm - 如何将一个负数和正数列表划分为最大数量的和为0的子集?