java 推断类型不符合上限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27245994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:29:12  来源:igfitidea点击:

Inferred type does not conform to upper bound(s)

javajava-8

提问by Jeffrey Tai

I'm doing a project in which I have to negate the pixels of a PPM file (image).

我正在做一个项目,我必须否定 PPM 文件(图像)的像素。

I implemented my negate function as such:

我这样实现了我的否定函数:

public PPMImage negate() 
{
    RGB[] negated = new RGB[pixels.length];
    System.arraycopy(pixels, 0, negated, 0, pixels.length);
    RGB[] negatedArr = Arrays.stream(negated).parallel().map(rgb -> rgb.neg(maxColorVal)).toArray(size -> new RGB[size]);
    return new PPMImage(width, height, maxColorVal, negatedArr);
}

With the neg(maxColorVal)function being defined as this:

neg(maxColorVal)函数被定义为这样的:

public void neg(int maxColorVal) 
{
    R = maxColorVal - R;
    G = maxColorVal - G;
    B = maxColorVal - B;
}

When I compile the code, I get the following error:

当我编译代码时,出现以下错误:

error: incompatible types: inferred type does not conform to upper bound(s)
RGB[] negatedArr = Arrays.stream(negated).parallel().map(rgb -> rgb.neg(maxColorVal)).toArray(size -> new RGB[size]);

inferred: void
upper bound(s): Object

The error points at the map() function. What am I doing incorrectly?

错误指向 map() 函数。我做错了什么?

采纳答案by Eran

Correction :

更正:

Your mapfunction expects a method that returns some reference type, but neghas a voidreturn type.

您的map函数需要一个返回某种引用类型但neg具有void返回类型的方法。

Try to change your negmethod to :

尝试将您的neg方法更改为:

public RGB neg(int maxColorVal) {
    R = maxColorVal - R;
    G = maxColorVal - G;
    B = maxColorVal - B;
    return this;
}