【问题标题】:Generic Object obtaining null value rather than a double value通用对象获取空值而不是双精度值
【发布时间】:2017-02-18 23:05:57
【问题描述】:

所以这段代码的目的是使用排序数组创建一个优先队列。

目前我有一个返回 null 的 C 对象,但我不认为它应该是。

相关代码:

    int n = 5;
    PQ<Double> pq1 = new PQasSortedArray<Double>(n); 
    double[] arr1 = new double[n];

    for(int i = 0; i < n; i++){
        Random num = new Random(); //Assigning random double values to the array
        arr1[i] = num.nextDouble();
    }

    for (int i=0; i < arr1.length; i++){
        pq1.insert(arr1[i]);
    }
    for (int i=arr1.length-1; i >=0 ; i--){
        arr1[i] = pq1.deleteMin();
    }

在我的 PQasSortedArray 类中,我有以下相关代码:

public class PQasSortedArray<C extends Comparable<? super C>> implements PQ<C> {
    private C[] arr; 
    private int currentSize;

public PQasSortedArray(int size) {
        arr = (C[]) new Comparable[size];
        currentSize = 0;
    }
public void insert(C data){ 
        arr[currentSize++] = data;
    }

public C min(){
    C tmp = arr[0]; // <-- This gives tmp a value of null, which is not what I want

    for(int i = 1; i < currentSize; i++) { // <-- This is skipped entirely as tmp is null
        if(tmp.compareTo(arr[i]) > 0) {
            tmp = arr[i];
        }
    }
    return tmp;

}

public C deleteMin(){ // <-- This also doesn't work as intended
    C tmp = arr[0];
    arr[0] = arr[currentSize-1];
    arr[currentSize-1] = null;
    currentSize--;
    return tmp;
}

所以我通常很不确定该怎么做。我不习惯使用泛型,所以我可能会遗漏一些非常明显的东西。

public interface PQ<C extends Comparable<? super C>> {
public boolean isFull();

public boolean isEmpty();

public void insert(C data); 

public C min();

public C deleteMin(); 

}

【问题讨论】:

  • 第一块中列出的代码是我一直用作测试的代码。给定 5 个 double 值,输出是 5 个 double 值,其索引更改为 -1,因此 0 成为数组的最后一个,1 成为 0,依此类推。它应该删除数组的最小值,并返回一个数组大小为 n-1,其中原始数组的大小为 n。我已经想出我想做的部分事情是在插入方法中从最小最大排序数组,但我仍然很困惑为什么 tmp 变为 null。
  • "这被完全跳过,因为 tmp 为空" 如果 tmp 为空,该行中没有任何内容会跳过。但是,如果 currentSize 为零...
  • 请贴出你的PQ界面。
  • 我现在已经将界面添加到帖子底部了。
  • 在循环内声明Random num = new Random(); 是一个错误。声明一次,让它生成伪随机序列,而不是一遍又一遍地从头开始序列。

标签: java arrays generics priority-queue


【解决方案1】:

测试结束时 currentSize == 1:

public C deleteFirst() { // <-- This also doesn't work as intended
    C tmp = arr[0];
    arr[0] = arr[currentSize - 1];
    arr[currentSize - 1] = null;
    currentSize--;
    return tmp;
}

变成:

public C deleteFirst() { // <-- This also doesn't work as intended
    C tmp = arr[0];
    arr[0] = arr[0];
    arr[0] = null;
    currentSize--;
    return tmp;
}

最后:arr[0] == null 和 currentSize == 0。

tmp = arr[0] == null:

public C min(){
    C tmp = arr[0]; // <-- This gives tmp a value of null, which is not what I want

    for(int i = 1; i < currentSize; i++) { // <-- This is skipped entirely as tmp is null
        if(tmp.compareTo(arr[i]) > 0) {
            tmp = arr[i];
        }
    }
    return tmp;

}

你需要这样的东西:

public C min() {
    if (currentSize == 0) {
        throw new RuntimeException("Size is 0.");
    }

    C tmp = arr[0]; // <-- This gives tmp a value of null, which is not what I want

    for (int i = 1; i < currentSize; i++) { // <-- This is skipped entirely as tmp is null
        if (tmp.compareTo(arr[i]) > 0) {
            tmp = arr[i];
        }
    }
    return tmp;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2014-06-11
    • 2010-09-27
    相关资源
    最近更新 更多