首页 > 解决方案 > 未定义的参数返回 INT

问题描述

当我像这样创建它时,我有这样的代码

public final class PhpArray extends AbstractMap
{
    private TreeMap t;
    private HashMap m;
    
    public PhpArray() {
        this.t = new TreeMap(Request.PHP_ARRAY_KEY_COMPARATOR);
        this.m = null;
    }
    
    @Override
    public Object put(final Object key, final Object value) {
        if (this.m != null) {
            return this.m.put(key, value);
        }
        try {
            return this.t.put(key, value);
        }
        catch (ClassCastException e) {
            this.m = new HashMap(this.t);
            this.t = null;
            return this.m.put(key, value);
        }
    }
    
    @Override
    public Set entrySet() {
        if (this.t != null) {
            return this.t.entrySet();
        }
        return this.m.entrySet();
    }
    
    public int arraySize() {
        if (this.t == null) {
            throw new IllegalArgumentException("The passed PHP \"array\" is not a sequence but a dictionary");
        }
        if (this.t.size() == 0) {
            return 0;
        }
        return 1 + this.t.lastKey();
    }
}

但是当我更新我的项目时,我在代码中遇到了错误

return 1 + this.t.lastKey();

错误是一个参数+未定义..为什么这样?以及如何解决问题?

标签: java

解决方案


TreeMap是一个泛型类,但在您问题的代码中,您使用它时没有类型参数。这意味着您的这行代码:

private TreeMap t;

本质上是这样的:

private TreeMap<Object, Object> t;

换句话说,t.lastKey()返回 anObject和运算符+不能使用,Object因为 anObject不是数字。

也许您的意思是调用方法size()而不是方法lastKey()

也许本教程会有所帮助?


推荐阅读