简体   繁体   中英

Java - I have a 2D array. How can I add blocks of it together?

I'm not sure if this is the best way to ask my question.

Basically, I have a 2D array that is being built from a text file. It takes the first two int's for the dimensions. Then fills the array with the remaining data. That part is working fine.

In my array, I need to add each value with each adjacent value. To determine which value, when added with all of its adjacent values, is the highest. I need to do the reverse also, to find the lowest.

What kind of loop or function could I use to accomplish this? I'l create a small example below.

2 4 3 7 8

1 5 7 9 2

2 9 2 5 7

So the 2 would become a 7, the 4 would become a 14, and so on. After the math is done I need to detect which coordinate in the array is the largest number.

For simplicity, lets use the example you provided. The array is 5 by 3. Lets call the array data Try this

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

Then loop through the arrays and compare the values.

My approach would be the following:

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

When given a 2D integer array, the method will compare every value with added neighbors to the current max value.

You explained your situation well but in future questions you should include what you already have in small blocks of code. :)

I did this for fun. Hope someone enjoys.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM