Question: Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the two lists, in

Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the two lists, in the following order:

  • First element from first list
  • First element from second list
  • Second element from first list
  • Second element from second list
  • Third element from first list
  • Third element from second list
  • . . .

If the lists do not contain the same number of elements, the remaining elements from the longer list should be placed consecutively at the end. For example, for a first list of [1, 2, 3, 4, 5] and a second list of [6, 7, 8, 9, 10, 11, 12] , a call of alternate(list1, list2) should return a list containing [1, 6, 2, 7, 3, 8, 4, 9, 5, 10, 11, 12].

Step by Step Solution

3.54 Rating (171 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public static List alternate List list1 List list... 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!