Java 二维阵列。查找相邻值之和
我很难想出一个在2d数组中找到相邻值之和的好方法。我需要找到索引位置上所有相邻值的总和。如果这个位置是一条边,我需要把它的值转换成和。因此,每个元素都有4个值构成其总和 例如 指数[0][0]处的元素=5.0。[0][1]=2和[1][0]=4。总数 索引[0][0]=5(左/自身镜像)+5(上/自身镜像)+ 2(右边)+4(下面)=16 我的代码如下。如能提出更好的建议,将不胜感激。此代码正在获取 边界排列:3 在注释后面的行上说//ERROR**Java 二维阵列。查找相邻值之和,java,arrays,sum,type-2-dimension,Java,Arrays,Sum,Type 2 Dimension,我很难想出一个在2d数组中找到相邻值之和的好方法。我需要找到索引位置上所有相邻值的总和。如果这个位置是一条边,我需要把它的值转换成和。因此,每个元素都有4个值构成其总和 例如 指数[0][0]处的元素=5.0。[0][1]=2和[1][0]=4。总数 索引[0][0]=5(左/自身镜像)+5(上/自身镜像)+ 2(右边)+4(下面)=16 我的代码如下。如能提出更好的建议,将不胜感激。此代码正在获取 边界排列:3 在注释后面的行上说//ERROR** Public static double [
Public static double [][] getSumWeight(double [][] map){
int base = 0;
//int count = getEnhancementLevel();
int row = map.length;
int col = map[0].length;
int newRow = row*2;
int newCol = col*2;
double [] [] sumMap = new double [row][col];
double [][] weightMap = new double [newRow][newCol];
//getting the corner sum weights
for (int i = 0; i < map.length; i++){
for (int j = 0; j < map[i].length; j++){
if (i == 0 && j == 0){
double result = 0;
result += (map[0][0])*2;
result += map[0][1];
result += map[1][0];
sumMap[0][0] = result;
System.out.println("corner" + sumMap[0][0]);
}
else if (i == 0 && j == map[0].length){
double result = 0;
result += map[0][map[0].length];
result += map[0][map[0].length-1];
result += map[1][map[0].length];
sumMap[i][j] = result;
}else if (i == map.length && j == 0){
}else if (i == map.length && j == map[map.length].length){
}
//getting the mid(s) of the top row
else if (i == 0 && j != 0 && j != map[0].length){
double result = 0;
result += map[i][j]; //top value or mirror
result += map[i][j-1]; // left value
//ERROR****
result += map[i][j+1]; // right value
result += map[i+1][j]; // bottom value
sumMap[i][j] = result;
}
//getting the mid(s) of the bottom row
else if (i == map.length && j != 0 && j != map[map.length].length){
double result = 0;
result += map[i][j];
result += map[i][j-1];
result += map[i][j+1];
result += map[i-1][j];
sumMap[i][j] = result;
}
//getting the mid(s) of the left most column
else if (j == 0 && i != 0 && i != map.length){
double result = 0;
result += map[i][j];
result += map[i-1][j];
result += map[i+1][j];
result += map[i][j+1];
sumMap[i][j] = result;
}
//getting the mid(s) of the right most column
else if (j == map[0].length && i != 0 && i != map.length){
double result = 0;
result += map[i][j];
result += map[i-1][j];
result += map[i+1][j];
result += map[i][j-1];
sumMap[i][j] = result;
}
//filling in all the remaining values
else{
double result = 0;
result += map[i-1][j];
result += map[i+1][j];
result += map[i][j-1];
result += map[i][j+1];
sumMap[i][j] = result;
}
}
}
for (int i = 0; i < map.length; i++){
for (int j = 0; j < map[i].length; j++){
System.out.println(sumMap[i][j]);
}}
return sumMap;
}
publicstaticdouble[]getSumWeight(double[]map){
int base=0;
//int count=getEnhancementLevel();
int行=map.length;
int col=map[0]。长度;
int newRow=行*2;
int newCol=col*2;
double[]sumMap=新的双精度[行][col];
double[]weightMap=newdouble[newRow][newCol];
//求角和权重
对于(int i=0;i
您将在以下行中收到ArrayOutOfBounds异常:
result += map[i][j+1]; // right value
因为您正在到达for循环中的最后一个位置map[i].length
,并访问下一个(越界)位置map[i][j+1]
请注意,您正在检查是否错误地到达了最后一个位置:
Example: map[0].length = 4 (0,1,2,3)
else if (i == 0 && j != 0 && j != map[0].length){ // j=3 but j!=4
result += map[i][j+1] //<- error
}
这是你的解决方案。我知道有很多
if-else
语句,但这是我最好的
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
if(i==0){
if(j==0){
sum[i][j] = a[i][j]*2 + a[i+1][j] + a[i][j+1];
}
else if(j==a[i].length-1){
sum[i][j] = a[i][j]*2 + a[i][j-1] + a[i+1][j] ;
}
else{
sum[i][j] = a[i][j] + a[i][j-1] + a[i+1][j] + a[i][j+1];
}
}
else if(i==a.length-1){
if(j==0){
sum[i][j] = a[i][j]*2 + a[i-1][j] + a[i][j+1];
}
else if(j==a[i].length-1){
sum[i][j] = a[i][j]*2 + a[i][j-1] + a[i-1][j] ;
}
else{
sum[i][j] = a[i][j] + a[i][j-1] + a[i-1][j] + a[i][j+1];
}
}
else if(j==0){
sum[i][j] = a[i][j] + a[i-1][j] + a[i+1][j] + a[i][j+1];
}
else if(j==a[i].length-1){
sum[i][j] = a[i][j] + a[i-1][j] + a[i+1][j] + a[i][j-1];
}
else{
sum[i][j] = a[i][j-1] + a[i-1][j] + a[i+1][j] + a[i][j+1];
}
}
}
for(int i=0;i算法要简单得多(我改变了第一个解决方案,因为我第一眼就理解了错误的需求);所有的逻辑都在第一个for中。我主要去掉了所有不需要的循环
public class MatrixAlgoritm {
public static double[][] getSumWeight(double[][] map) {
int row = map.length;
int col = map[0].length;
double[][] sumMap = new double[row][col];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
double result = 0;
result += map[Math.max(0, i - 1)][j];
if (i == map.length - 1) {
result += map[i][j];
} else {
result += map[i + 1][j];
}
result += map[i][Math.max(0, j - 1)];
if (j == map[0].length - 1) {
result += map[i][j];
} else {
result += map[i][j + 1];
}
sumMap[i][j] = result;
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.println(sumMap[i][j]);
}
}
return sumMap;
}
public static void main(String[] args) {
double[][] baseMap = {{2, 10, 7}, {4, 5, 8}, {5, 6, 9}};
getSumWeight(baseMap);
}
}
公共类MatrixAlgoritm{
公共静态双精度[][]getSumWeight(双精度[][]映射){
int行=map.length;
int col=map[0]。长度;
double[]sumMap=新的双精度[行][col];
对于(int i=0;i public class MatrixAlgoritm {
public static double[][] getSumWeight(double[][] map) {
int row = map.length;
int col = map[0].length;
double[][] sumMap = new double[row][col];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
double result = 0;
result += map[Math.max(0, i - 1)][j];
if (i == map.length - 1) {
result += map[i][j];
} else {
result += map[i + 1][j];
}
result += map[i][Math.max(0, j - 1)];
if (j == map[0].length - 1) {
result += map[i][j];
} else {
result += map[i][j + 1];
}
sumMap[i][j] = result;
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.println(sumMap[i][j]);
}
}
return sumMap;
}
public static void main(String[] args) {
double[][] baseMap = {{2, 10, 7}, {4, 5, 8}, {5, 6, 9}};
getSumWeight(baseMap);
}
}
int sum[][] = new int[c][c];
for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[i].length; j++)
{
int result = 0;
if(j > 0)
result += arr[i][j-1];
if(j < arr[i].length - 1)
result += arr[i][j+1];
if(i > 0)
result += arr[i-1][j];
if(i > 0 && j > 0)
result += arr[i-1][j-1];
if(i > 0 && j < arr[i].length - 1)
result += arr[i-1][j+1];
if(j > 0 && i < arr.length -1)
result += arr[i+1][j-1];
if(j < arr[i].length - 1 && i < arr.length -1)
result += arr[i+1][j+1];
if(i < arr.length -1)
result += arr[i+1][j];
sum[i][j] = result;
}
}
**//The function receives a matrix and replaces each value in the matrix with the value of the sum of its neighbors
public static int[][] sumOfNeighbours(int[][] mat) {
int[][] sum = new int[mat.length][mat[0].length];
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
if (i == 0) {
if (j == 0) {
sum[i][j] = mat[i + 1][j + 1] + mat[i][j + 1] + mat[i + 1][j];//dells with the top left corner
} else if (j == mat[i].length - 1) {
sum[i][j] = mat[i + 1][j - 1] + mat[i][j - 1] + mat[i + 1][j];//dells with the top right corner
} else {
sum[i][j] = mat[i][j - 1] + mat[i][j + 1] + mat[i + 1][j + 1] + mat[i + 1][j - 1] + mat[i + 1][j];//top middles
}
} else if (i == mat.length - 1) {
if (j == 0) {
sum[i][j] = mat[i - 1][j] + mat[i - 1][j + 1] + mat[i][j + 1];//dells with the bottom left corner
} else if (j == mat[i].length - 1) {
sum[i][j] = mat[i - 1][j] + mat[i][j - 1] + mat[i - 1][j - 1];//dells with the bottom right corner
} else {
sum[i][j] = mat[i - 1][j] + mat[i - 1][j - 1] + mat[i - 1][j + 1] + mat[i][j + 1] + mat[i][j - 1];//dells with the bottom middles
}
} else if (j == 0) {//first column
if ((i != 0) && (i != mat.length - 1))
sum[i][j] = mat[i - 1][j] + mat[i + 1][j] + mat[i + 1][j + 1] + mat[i - 1][j + 1] + mat[i][j + 1];//dells with the left middles
} else if (j == mat[i].length - 1) {//last column
if ((i != 0) && (i != mat.length - 1))
sum[i][j] = mat[i - 1][j] + mat[i + 1][j] + mat[i - 1][j - 1] + mat[i][j - 1] + mat[i + 1][j - 1];//dells with the right middles
} else {
sum[i][j] = mat[i][j - 1] + mat[i][j + 1] + mat[i + 1][j] + mat[i + 1][j - 1] + mat[i + 1][j + 1] + mat[i - 1][j] + mat[i - 1][j - 1] + mat[i - 1][j + 1];//dells with the all the rest
}
}
}
return sum;
}**