A List is an ordered sequence of elements stored together to form a collection. A list is a structure to perform both positional access and index-based retrieval of the elements. Lists can be used to store duplicate as well as null elements. Running Total of a sequence of the elements is the summation of the previous sum of the elements and the current integer value received. Lists can be declared and created using an in-built interface <<java.util>>
Storing the first index element of the list into the running total as it is. The previous sum of the elements is initialized to the first index element of this list. And then, with each new element, we add it to the previous sum. This updated sum value becomes the running total until the corresponding index. It is then added to the running total list. Below the procedure is described:
Procedure: Steps followed to compute sum in list
- Step 1: Declaring list for storing running total.
- Step 2: Copy the first element of the original list to the running total List.
- Step 3: Initially, declaring sum to the 0th element of the List.
- Step 4: Iterating over the list starting with the 1st index of List.
- Step 5: Adding the current element to the previous sum.
- Step 6: Copying the previous sum to the running total sum.
- Step 7: Iterating over the elements of the running total List using hasNext() method which holds true till there is a single element remaining in the List.
- Step 8: Print the List which will be running the total List.
Implementation: The following java program is used to illustrate the computation of the running total of the list of elements.
Original List : 2 4 6 8 10 12 Running total List of Original List: 2 6 12 20 30 42
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.