简体   繁体   中英

How can I create a matrix with Java and add all its elements to a sum?

I need to write a code in Java which creates a 3000X3000 matrix, where each element is a integer type. After this I need to add all the elements.

I was given this part of code:

public class matrix {
    public static void main(String[] args) {

    long sum = 0;
    int dimension = 3000;
    int i, j;
    int matrix[][] = new int[dimension][dimension];
    ...
        for (i=0; i<dimension; i++) {
                for (j=0; j<dimension; j++) {
                    sum = sum + matrix[i][j];
            }
        }
    }
}

But since I never worked with arrays before, actually we just talked about arrays today the first time. I don't really get how I need to customize this code, so that I end up with the sum of all the elements inside this matrix.

You can use two loops and iterate over array and store integer value in them.

public static void main(String[] args) {
        long sum = 0;
        int dimension = 3000;
        int i, j;
        int matrix[][] = new int[dimension][dimension];

        for (i = 0; i < dimension; i++) {
            for (j = 0; j < dimension; j++) {
                matrix[i][j] = 1; // Number you want to store, could be a scanner input
            }
        }

        for (i = 0; i < dimension; i++) {
            for (j = 0; j < dimension; j++) {
                sum = sum + matrix[i][j];
            }
        }

        System.out.println(sum);
    }

An array is simply a list of something. A matrix, in Java, would be a list of lists, so to access one point in the grid, you access arr[y][x] , where x is the x position, and y is the y position.

So then, to add all of it together, you can use code with this structure:

int sum = 0;                              // Define the sum to add to
int[][] grid = new int[] {                // Define the grid
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
};
for (int x = 0; x < grid[0].length; x++)  // Loop through X indicies
    for (int y = 0; y < grid.length; y++) // Loop through Y indicies
        sum += grid[y][x];                // Add value to the sum

This code loops through every integer, and adds it to the sum. Remember, the first index is the row, so therefore y , and the second is the column, so therefore x . You loop through both indicies, catching every number stored in the grid and storing it.

I have modified iNan's answer a bit:

public static void main(String[] args) {
    long sum = 0;
    int dimension = 3000;
    int i, j;
    int matrix[][] = new int[dimension][dimension

    for (i = 0; i < dimension; i++) {
        for (j = 0; j < dimension; j++) {
            matrix[i][j] = 1; // Number you want to store, could be a scanner input
            sum = sum + matrix[i][j];
        }
    }

    System.out.println(sum);
}

You don't need to iterate separately over the matrix filling the entries and then iterating over it again summing all the things up. You can fill and sum up in one step accelerating your program a bit. With large matrices it could speed up the program significantly.

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