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

时间:2015-02-03 22:36:07

标签: java arrays

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

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

在我的数组中,我需要为每个相邻值添加每个值。要确定哪个值与其所有相邻值一起添加时最高。我也需要反过来找到最低的。

我可以使用什么样的循环或函数来完成此任务?我在下面创建一个小例子。

2 4 3 7 8

1 5 7 9 2

2 9 2 5 7

因此2将成为7,4将成为14,依此类推。数学完成后,我需要检测数组中哪个坐标是最大的数字。

3 个答案:

答案 0 :(得分:0)

为简单起见,我们使用您提供的示例。数组是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;
    }
}

然后遍历数组并比较值。

答案 1 :(得分:0)

我的方法如下:

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;
}

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

答案 2 :(得分:0)

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

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

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();
}
}