java - 不兼容的类型:RGBColor [][] 无法转换为 Double [][] -Error
问题描述
我需要一些帮助来解决让我发疯的事情。关于代码的一些信息-
- 像素是红、绿、蓝3种颜色。
- RGBColor[][] 表示一个像素数组。
- RGBImage[][] 表示一个 RGBColor 数组。RGBImage[][] 数组的每个单元格中都有 RGBColor 对象。
- convertToGrayscale() 是一种将每个像素转换为其灰度值的方法。
- 还有一个 RGBImage 类的 toString() 方法可以打印出带有所有值的图像 - RGBColor 对象数组。
问题- 我希望 toGrayscaleArray() 方法使用 convertToGrayscale() 方法。所以我构建了一个 for 循环,将数组的每个单元格转换为灰度。但是当我尝试返回现在已转换的 _rgbimage 时,我得到编译器错误 -不兼容的类型:RGBColor [][] 无法转换为 Double [][]
任何帮助将非常感激。
/** returns grayscale representation of the image
* @return grayscale representation of the image
*/
public double [][] toGrayscaleArray()
{
int row = _rgbimage.length;
int col = _rgbimage[0].length;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
{
_rgbimage[i][j].convertToGrayscale();
}
return _rgbimage; //THE COMPILER ERROR IS HERE
}
toString() - 该方法已经过测试并且可以正常工作。
/** creates a string of matrix which represents the image, in the following format:
* (red,green,blue) (red,green,blue) (red,green,blue)
* (red,green,blue) (red,green,blue) (red,green,blue)
* (red,green,blue) (red,green,blue) (red,green,blue)
* @return toString
*/
public String toString ()
{
int rows = _rgbimage.length;
int cols = _rgbimage[0].length;
String imageMatrix = new String("");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (j < cols-1) //checks if it's the last cell on the row
imageMatrix += _rgbimage[i][j] + " "; //if not last cell - add space
else
imageMatrix += _rgbimage[i][j]; //if last cell - don't add space
}
imageMatrix += "\n";
}
return imageMatrix;
}
解决方案
推荐阅读
- javascript - 如何在 javascript 中替换 XMLHttpRequest 以解析移动设备上的文件?
- csv - logstash 从 imap 读取 csv 附件
- mysql - 无法使用 mySQL 连接到数据库
- c - 从特定地址调用 mmap
- android - react-native 应用程序因 react-native-firebase 云通知而崩溃
- android - 在自定义编辑文本中覆盖 onSelectionChanged 时出现 NullPointerException
- html - 网站手机版和媒体查询版不一样
- bash - 编写一个模仿'return'内置的bash函数
- java - 批量提取到@ElementCollection
- swift - 如何在应用程序中使用之前创建的视图文件?