我正在尝试用Java创建一个图像的2D数组。
这是我到目前为止所拥有的:
public int[][] GetArray() { //NetBeans is saying 'Illegal Start of Expression' on this line
getimage data;
getwidth;
getheight;
int[][] array = new int[width][height];
for (loop through width) {
for (loop through height) {
array[q][p] = raster.getSample(p, q, 0);
}
}
return array;
我尝试将返回部分设置为: -
return array[][];
但是这产生了一个错误,说无法找到符号。
我对Java比较陌生,我真的想快速好起来,如果你能帮助我,我真的很感激。
答案 0 :(得分:3)
如果您想要返回一个数组,请按照这样做
return array; // CORRECT
你在做什么是不正确的。
return array[][]; // INCORRECT
您的功能应如下所示
public class MyClass
{
// main is a method just like GetArray, defined inside class
public static void main(String[] args)
{
// do something
}
// other methods are defined outside main but inside the class.
public int[][] GetArray() {
int width = 5; // change these to your dimensions
int height = 5;
int[][] array = new int[width][height];
int q,p;
for(q=0;q<width;q++)
{
for(p=0;p<height;p++)
{
array[q][p] = raster.getSample(p, q, 0);
}
}
return array;
}
}
答案 1 :(得分:0)
当您返回array
时,不应使用
return array[][];
你不应该使用方括号[][]
。在return
语句中,我们不应该提到数组的维度
改为使用
return array;
这是正确的方法