How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java

In a two-dimensional array, declare two [] as [] [].

sample


                int[][] score = {
                { 1, 2, 3, 4, 5 },          //Line 0
                { 10, 20, 30, 40, 50 },     //The first line
                { 100, 200, 300, 400, 500 } //2nd line
	        };
        System.out.println(score[Line][Column]);
        System.out.println(score[0][0]);    //0th row, 0th column
        System.out.println(score[1][1]);    //1st row, 1st column
        System.out.println(score[2][3]);    //2nd row, 3 columns


	//Note that it starts at row 0, column 0


		int[][] array = {
					{1,2,3,4},
					{10,20,30,40,50,60},
					{1000,10000}
				};

		//Outputs all integer elements contained in the 2D array array in order
for(int i=0; i<array.length; i++) { //Array name.Get the number of elements in a column (vertical direction) by length
	for(int s=0; s<array[i].length; s++) { //Array name[i].Get the number of elements in a row (horizontal direction) by length
		System.out.println(array[i][s]); //array[Vertical][side]
			}
		}

Console results 1 2 3 4 10 20 30 40 50 60 1000 10000

sample


		int sum = 0;
		for(int i=0; i<array.length; i++) {
			for(int s=0; s<array[i].length; s++) {
				sum += array[i][s];
			}
		}
		System.out.println(sum);
	}

Console results 11220

sample


		int sum = 0;
		int count = 0;
		for(int i=0; i<array.length; i++) {
			for(int s=0; s<array[i].length; s++) {
				sum += array[i][s];
				count++;
			}
		}
		System.out.println(sum/count);
	}

Console results 935

Recommended Posts

How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
How to find the total score and average score
How to find the average angle
Try to imitate the idea of a two-dimensional array with a one-dimensional array
How to find out the Java version of a compiled class file
How to find the total number of pages when paging in Java
How to change the value of a variable at a breakpoint in intelliJ
How to find the cause of the Ruby error
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
[Ruby] How to find the sum of each digit
[Java] How to get the maximum value of HashMap
Display the average value of the evaluation as a star
[ruby] How to assign a value to a hash by referring to the value and key of another hash
How to divide a two-dimensional array into four with ruby
Difference between Java and JavaScript (how to find the average)
How to retrieve the hash value in an array in Ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to change the setting value of Springboot Hikari CP
[Ruby] How to retrieve the contents of a double hash
How to add elements without specifying the length of the array
How to add the same Indexes in a nested array
[jsoup] How to get the full amount of a document
How to specify an array in the return value / argument of the method in the CORBA IDL file
[Swift5] How to get an array and the complement of arrays
How to display 0 on the left side of the standard input value
How to output the value when there is an array in the array
[Java] How to search for a value in an array (or list) with the contains method
[chown] How to change the owner of a file or directory
How to check for the contents of a java fixed-length string
How to make a Java array
How to increment the value of Map in one line in Java
How to output the sum of any three numbers, excluding the same value
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
How to return a value from Model to Controller using the [Swift5] protocol
[Java] How to turn a two-dimensional array with an extended for statement
How to convert an array of Strings to an array of objects with the Stream API
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
Android development, how to check null in the value of JSON object
How to add a new hash / array
How to determine the number of parallels
How to sort the List of SelectItem
How to find the tens and ones
How to pass the value to another screen
Find the difference from a multiple of 10
[Swift] How to get the number of elements in an array (super basic)
How to get the ID of a user authenticated with Firebase in Swift
How to convert a value of a different type and assign it to another variable
How to find the distance and angle between two points on a plane
How to make a unique combination of data in the rails intermediate table
Customize how to divide the contents of Recyclerview
Make a margin to the left of the TextField
How to delete a controller etc. using a command
Set the time of LocalDateTime to a specific time
How to create pagination for a "kaminari" array
How to get today's day of the week
Output of how to use the slice method
How to display the result of form input
[Java] How to get the authority of the folder
Speed comparison when the value side of Hash wants to retrieve all with array
A memo about the types of Java O/R mappers and how to select them