![](/img/trans.png)
[英]having trouble making a method that creates a 2d array out of a T type unrolled linked list
[英]Having trouble printing out a filtered 2d array with a method call
編輯:修復了代碼中的問題,但出現了新的、新穎的錯誤。 發布到新線程以獲取見解。
我的家庭作業有一些問題,需要我打印出二維數組的過濾副本。 我需要創建一個嵌套的 for 循環,該循環調用另一個方法(它將索引的鄰居存儲在一維數組中),找到一維數組的平均值,然后打印出一個帶有平均值的二維數組。
我能夠編寫方法“getNeighbors”來獲取索引的鄰居,但是當我將該方法調用到 main 中時,我得到了 6 個錯誤:
Lab5.java:45: 錯誤: 找不到符號 getNeighbors(row, col, imageData); ^ 符號:可變圖像數據位置:class Lab5
Lab5.java:47: 錯誤: 找不到符號 (int i=0; i<nebs.length; i++){ ^ 符號: 變量 nebs 位置: class Lab5
Lab5.java:48: 錯誤: 找不到符號 sum += nebs[i]; ^ 符號:可變 nebs 位置:class Lab5
Lab5.java:50: 錯誤: 找不到符號 int average = sum / nebs[i].length; ^ 符號:可變 nebs 位置:class Lab5
Lab5.java:50: 錯誤: 找不到符號 int average = sum / nebs[i].length; ^ 符號:變量 i 位置:class Lab5
Lab5.java:65: 錯誤: 不兼容類型: int[][] 不能轉換為 int[] } return copyImage;
我不知道這是否對我造成了精神疲勞,但我不知道如何正確調用該方法來提供一維鄰居數組,然后在不使用 getNeighbors 方法中的變量的情況下獲取平均值。
這是我的代碼:
public static int[][] applyFilter1(int[][] imageData){
int[][] filtered = new int[imageData.length][imageData[0].length];
for (int row=0; row<filtered.length;row++){
for (int col=0;col<filtered[row].length;col++){
getNeighbors(row, col, imageData);
int sum = 0;
for(int i=0; i<nebs.length; i++){
sum += nebs[i];
}
int average = sum / nebs[i].length;
filtered[row][col] = average;
}
}
return filtered;
}
public static int[] getNeighbors(int row, int col, int[][] imageData){
//find neighbors of current index
int [][] copyImage = new int[imageData.length][imageData[0].length];
try{
for(int r=0; r<imageData.length; r++){
for(int c=0; c<imageData[r].length; c++){
imageData[r][c] = copyImage[r][c];
}
} return copyImage;
} catch(Exception e){
System.out.println("Array copy not successful");
}
//handles the top row of the array
if(row==0){//if copyImage[0].length
if(col==0){//handles upper left corner of array
int[] nebs = {copyImage[row][col], copyImage[row+1][col],
copyImage[row][col+1]};
return nebs;
}
else if(col==copyImage[row].length-1){//handles upper right corner of array
int[] nebs = {copyImage[row][col], copyImage[row][col-1],
copyImage[row+1][col]};
return nebs;
}
else{//handles top row of array between corners
int[] nebs = {copyImage[row][col], copyImage[row][col-1],
copyImage[row+1][col], copyImage[row][col+1]};
return nebs;
}
}
//handles the bottom row of the array
else if(row==copyImage.length-1){//if the row is at max value
if(col==0){//handles botton left corner of array
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row][col+1]};
return nebs;
}
else if(col==copyImage[row].length-1){//handles bottom right corner of array
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row][col-1]};
return nebs;
}
else{//handles bottom row of array
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row][col-1], copyImage[row][col+1]};
return nebs;
}
}
//handles leftmost column of array
else if(col==0){//if col=0 and row increases
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row+1][col], copyImage[row][col+1]};
return nebs;
}
//handles rightmost column of array
else if(col==copyImage[row].length-1){//if col=max value and row increases
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row+1][col], copyImage[row][col-1]};
return nebs;
}
//handles values in the body of the array
else{
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row+1][col], copyImage[row][col-1],
copyImage[row][col+1]};
return nebs;
}
}
我該怎么做才能使它正確編譯和執行?
也許您打算在getNeighbors
調用中寫入image
( int[][]
參數的名稱applyFilter1
),而不是imageData
? 也許您還打算將 getNeighbors 的結果保存在名為getNeighbors
的int[]
nebs
?
int[][] nebs = getNeighbors(row, col, image);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.