Sum of elements within nested arrays

Language: Java


The data for the number of steps carried out within a year is found within an array of seven elements (each element an array representing a day of the week). Add up all the steps within the arrays nested within the weekday array and then multiply it by 20 to estimate how many steps would be carried out in 20 years.


int [] [] stairs –> an 7 element array of arrays, the nested arrays being any length.


import java.util.ArrayList; public class StairsInTwenty{ public static long stairsIn20(int[][] stairs) { int total = 0; ArrayList weekDay = new ArrayList(); for (int [] value:stairs){ for (int valueTwo:value ){ total+=valueTwo; } weekDay.add(total); total = 0; } for (int value:weekDay){ total+=value; } return total*20; } }


The creation of the weekday arrayList wasn’t required and the values could simply be added on within the second loop to reduce code.