This is the 15th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Like it and see. Make it a habit. Wechat search [a coding] follow this programmer who is struggling in the Internet.

This article is collected on Github – Technical expert Training, which contains my learning route, series of articles, interview question bank, self-study materials, e-books, etc.

Topic describes

Difficulty: Easy

Calculates the sum of the rows in a two-dimensional array and finds the row with the largest value.

knowledge

  • 2 d array
  • Double loop

Their thinking

1. Two-dimensional arrays

A two-dimensional array is essentially an array of arrays as array elements, that is, “arrays of arrays”. You can think of it as a square array.

Define an array

int brr [] []=new int[] [3]; // Lines in a two-dimensional array can be omitted, at least to write columns
Copy the code

Add elements

brr[0] [0] =1; // Subscripts also start at 0, and can be assigned either at the specified position or as a whole.
brr={
  {1.2.3},
  {5.6.7},
  {9.10.11}};Copy the code

To iterate over

/ / traverse line
for(int i=0; i<3; i++){/ / traverse
     for(int j=0; j<3;j++){
       System.out.println(brr[i][j]);
     }
} 	 
Copy the code

2. Double cycle

Similar to the loop loop above.

In practical applications, double loop can solve most problems, but its time complexity is O(n^2), avoid using.

Code implementation

/** * Calculates the sum of each row in a two-dimensional array and finds the row with the largest value. * /
public class question_10 {
    public static void main(String args[]) {
        int myTable[][] = {
                {22.34.45.11.33.5.92},
                {17.9.27.31.46.54.88},
                {98.81.64.62.15.14.23},
                {54.43.55.1.22.9.33}};int sum, max, maxRow=0;
        max = 0;
        for (int row=0; row<4; row++) {
            sum = 0;
            for (int col=0; col<7; col++)
                sum += myTable[row][col];
            if (sum > max) {
                max = sum;
                maxRow = row;
            }
        }
        System.out.println("Row: " + maxRow + " value: "+ max); }}Copy the code

The output

Expand the summary

In the actual development, we use more collection, on the collection of knowledge, you can see my article: DACHang interview burst – collection.

The last

One foot is difficult to go, one hand is difficult to sing, a person’s power is limited after all, a person’s journey is doomed to be lonely. When you set a good plan, with full of blood ready to set out, must find a partner, and Tang’s monk to the West, the master and his disciples four people united as one to pass the ninety-eight difficult. So,

If you want to learn Java well

Want to go to company

Want to get high salary

Want to have a group of like-minded partners

Please join the technical exchange