Recap - All About Arrays
Part of the Logic & Flow section of Coddy's Java journey — lesson 9 of 59.
Challenge
EasyCreate a method named arrayOperations that takes a 2D array of integers (matrix) as input and performs the following operations:
- Calculate the sum of all elements in the matrix.
- Find the maximum element in the matrix.
- Create a new 1D array that contains the sums of each row in the matrix.
- Print to the console using the following format:
Sum: 78
Maximum: 12
Row Sums: [10, 26, 42]Try it yourself
import java.util.Arrays;
// Write your code only inside the class. Do not write main() or any code outside this class.
class ArrayOperations {
public static void arrayOperations(int[][] matrix) {
// Write your code here
System.out.println("Sum: " + sum);
System.out.println("Maximum: " + max);
System.out.println("Row Sums: " + Arrays.toString(rowSums));
}
}All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays2HashMap Part 1
What is a HashMap?Declare a HashMapAccessing ValuesCheck If Key ExistsModifying DictionariesRecap - HashMap