Question: Write a method called compress that replaces every pair of elements in the list with a single element equal to the sum of the pair.

Write a method called compress that replaces every pair of elements in the list with a single element equal to the sum of the pair. If the list is of odd size, leave the last element unchanged. For example, if the list stores [1, 7, 3, 9, 4, 6, 5], your method should change it to store [8, 12, 10, 5] (1 + 7, and 3 + 9, then 4 + 6, then 5).

Step by Step Solution

3.43 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public void compress for int i 0 ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Building Java Programs A Back to Basics Approach Questions!