Java-我有一个2D数组。我如何才能将它的块添加到一起?

Java-我有一个2D数组。我如何才能将它的块添加到一起?,java,arrays,Java,Arrays,我不确定这是否是问我问题的最佳方式 基本上,我有一个从文本文件构建的2D数组。 对于维度,它需要前两个int。然后用剩余的数据填充数组。那部分很好用 在数组中,我需要将每个值与每个相邻值相加。确定与所有相邻值相加时,哪个值最高。我也需要做相反的,找到最低的 我可以使用什么样的循环或函数来实现这一点?我将在下面创建一个小示例 24378 15792 29257 所以2会变成7,4会变成14,依此类推。完成数学运算后,我需要检测数组中哪个坐标是最大的数字。为了简单起见,让我们使用您提供的示例。这个数

我不确定这是否是问我问题的最佳方式

基本上,我有一个从文本文件构建的2D数组。 对于维度,它需要前两个int。然后用剩余的数据填充数组。那部分很好用

在数组中,我需要将每个值与每个相邻值相加。确定与所有相邻值相加时,哪个值最高。我也需要做相反的,找到最低的

我可以使用什么样的循环或函数来实现这一点?我将在下面创建一个小示例

24378

15792

29257


所以2会变成7,4会变成14,依此类推。完成数学运算后,我需要检测数组中哪个坐标是最大的数字。

为了简单起见,让我们使用您提供的示例。这个数组是5乘3。让我们调用数组数据,然后尝试以下操作

int totals[5][3];
for(int x = 0;x<5;x++){
    for(int y = 0;y<5;y++){
        int total = data[x][y]
        if(x>0){
            total+=  data[x-1][y];
        }
        if(x<4){
            total+=  data[x+1][y];
        }
        if(y>0){
            total+=  data[x][y-1];
        }
        if(y<2){
            total+=  data[x][y+1];
        }
        totals[x][y] = total;
    }
}
int总计[5][3];

对于(int x=0;x我的方法如下:

public int largeNeighbor(int[][] numbers) {
    int max = 0;
    for (int i = 0; i < numbers.length ; i++) {
        for (int j = 0; j < numbers[0].length; j++) {
            int temp = numbers[i][j];
            if (i > 0) {
                temp += numbers[i-1][j];
            }
            if (i < numbers.length - 1) {
                temp += numbers[i+1][j];
            }
            if (j > 0) {
                temp += numbers[i][j-1];
            }
            if (j < numbers[0].length - 1) {
                temp += numbers[i][j+1];
            }
            if (temp > max) {
                max = temp;
            }
        }
    }
    return max;
}
public int largeNeighbor(int[][]个数字){
int max=0;
for(int i=0;i0){
温度+=数字[i-1][j];
}
如果(i0){
温度+=数字[i][j-1];
}
if(j<数字[0]。长度-1){
温度+=数字[i][j+1];
}
如果(温度>最大值){
最大值=温度;
}
}
}
返回最大值;
}

当给定一个2D整数数组时,该方法会将每个添加了相邻项的值与当前的最大值进行比较。

您已经很好地解释了您的情况,但在以后的问题中,您应该在小代码块中包含您已有的内容。:)

我这样做是为了好玩。希望有人喜欢

import java.lang.ArrayIndexOutOfBoundsException;
import java.util.Random;

public class HelloWorld{

int smallest = 10000;
int largest = -1;

int xCoords_small = -1;
int yCoords_small = -1;

int xCoords_large = -1;
int yCoords_large = -1;

//Make it as big as you want!!!!!
int iSize = 5;    
int jSize = 3;

int[][] totals = new int[iSize][jSize];
int[][] yourNumbers = new int[iSize][jSize];

Random r = new Random();

//Initializes the array. With random numbers. Yours would read in the
//the file here and initialize the array.
public HelloWorld(){
     for(int i = 0; i < iSize; i++){
        for(int j = 0; j < jSize; j++){
            yourNumbers[i][j] = r.nextInt(10);
        }
     }
}

//Calculates the total and whether or not it's the largest number and 
//tracks position in array and the total number.
//It has crumby error catching but this way you can make your array 
//as big as you want without needing to change anything but the two
//two size variables.
public void calculate(){
    for(int i = 0; i < iSize; i++){
        for(int j = 0; j < jSize; j++){
            int total = 0;
            try{
                total += yourNumbers[i][j];
            }catch(ArrayIndexOutOfBoundsException ex ){
                //do nothing
            }
            try{
                total += yourNumbers[i-1][j];
            }catch(ArrayIndexOutOfBoundsException ex){
                //do nothing
            }
            try{
                total += yourNumbers[i][j-1];
            }catch(ArrayIndexOutOfBoundsException ex){
                //do nothing
            }
            try{
                total += yourNumbers[i+1][j];
            }catch(ArrayIndexOutOfBoundsException ex){
                //do nothing
            }
            try{
                total += yourNumbers[i][j+1];
            }catch(ArrayIndexOutOfBoundsException ex){
                //do nothing
            }
            totals[i][j] = total;
            if(total > largest){
                largest = total;
                xCoords_large = i;
                yCoords_large = j;
            }
            if(total < smallest){
                smallest = total;
                xCoords_small = i;
                yCoords_small = j;
            }
            System.out.println(total);
        }
    }
    System.out.println(largest + " = Largest Total and it's beginning number in your 2D array. " + xCoords_large+ "," + yCoords_large+ " Its value = " +  yourNumbers[xCoords_large][yCoords_large]);
    System.out.println(smallest + " = Smallest Total and it's beginning number in your 2D array. " + xCoords_small + "," + yCoords_small + " Its value = " + yourNumbers[xCoords_small][yCoords_small]);

}

public static void main(String []args){
    HelloWorld hw = new HelloWorld();
    hw.calculate();
}
}
导入java.lang.ArrayIndexOutOfBoundsException;
导入java.util.Random;
公共类HelloWorld{
int=10000;
int最大=-1;
int xCoords_small=-1;
int yCoords_small=-1;
int xCoords_large=-1;
int yCoords_large=-1;
//让它像你想要的那样大!!!!!
int-iSize=5;
int-jSize=3;
int[][]总计=新的int[iSize][jSize];
int[][]yourNumbers=newint[iSize][jSize];
随机r=新随机();
//初始化数组。使用随机数。您的将在
//在此处输入文件并初始化数组。
公共HelloWorld(){
for(int i=0;i最大值){
最大=总数;
xCoords_large=i;
yCoords_large=j;
}
如果(总数<最小值){
最小值=总值;
xCoords_small=i;
yCoords_small=j;
}
系统输出打印项次(总计);
}
}
System.out.println(最大+“=最大总数及其在2D数组中的开始数。”+xCoords\u large+”,“+yCoords\u large+”其值=“+yourNumbers[xCoords\u large][yCoords\u large]);
System.out.println(minimate+“=最小总数及其在2D数组中的起始数字。“+xCoords\u small+”,“+yCoords\u small+”其值=“+yourNumbers[xCoords\u small][yCoords\u small]);
}
公共静态void main(字符串[]args){
HelloWorld hw=新HelloWorld();
hw.calculate();
}
}

您的实际问题是什么?我们不只是为您编写代码。所以2变成了
2+4+1=7
?它包括元素本身?@Alex,我不需要你为我写代码。我只是想知道在某个库中是否有一个函数,用于提取数组中的相邻数字进行相加瑞安,是的。它包括元素本身和所有相邻的数字。但是,在像第一个元素一样添加相邻元素后,下一个元素将添加第一个元素的原始值。稍微调整一下,这对我来说是可行的。非常感谢您和任何发布类似代码的人。这与上面的内容基本相同,我只是先读了一下。最后,它进行了一些轻微的调整。非常感谢!哇,太棒了,谢谢!上面的帖子解决了我的问题,但这确实有助于理解它是如何工作的。如果我能投票,我会的,但我还不能。无论如何谢谢你!