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 sit 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). Use the two lists given here. java
Code:
import java.io.*;
public class Main
{
public static void main(String args[])
{
int array1[] = {1,2,3,4,5}; // Array1
int array2[] = {6, 7, 8, 9, 10, 11, 12}; // Array 2
int New[] = new int[array1.length+array2.length]; // New array with
length of both above arrays
New=alternate(array1,array2,New); // Calling function to insert
elements to new array
// Printint new array
for(int i=0;i<New.length;i++){
System.out.print(New[i]+" ");
}
System.out.println();
}
public static int[] alternate(int[] a,int[] b, int[] New){
int index=0; // index for new array
int i=0,j=0; // index for array1 and array2
// run loop until insert all elements in new array
while(index<New.length){
New[index++]=a[i++]; // inserting element from array1 and
increasing their index
New[index++]=b[j++]; // inserting element from array2 and
increasing their index
if(i==a.length){ // if index of array1 reaches ending
break; // break loop
}
if(j==b.length){ // if index of array2 reaches ending
break; // Break loop
}
}
//if all elements of array1 not inserted
if(i<a.length){
for(int ii=i;ii<a.length;ii++){
New[index++]=a[ii];
}
}
//if all elements of array2 not inserted
if(j<b.length){
for(int jj=j;jj<b.length;jj++){
New[index++]=b[jj];
}
}
return New; // Returning new array
}
}
SCREENSHOT

OUTPUT
NOTE:
IF ANY DOUBTS ASK IN COMMENTS I WILL EXPLAIN YOU..
PLEASE CONSIDER MY WORK AND GIVE UP VOTE
DON'T FORGOT .....
Write a method called alternate that accepts two Lists as its parameters and returns a new...
Write a java method merge that accepts two arrays of integers and returns a new array containing all elements of the first array followed by all elements of the second. int[] a1 = {12, 34, 56}; int[] a2 = {7, 8, 9, 10}; int[] a3 = merge(a1, a2); System.out.println(Arrays.toString(a3)); // [12, 34, 56, 7, 8, 9, 10]
[2.5pts] Write the function connect(listl, list2, k) that takes two lists, listl and list2, and a non- negative integer k that is less than or equal to the length of listl. It returns (not prints) a new list containing the first k elements of list1, then all elements of list2, then the remaining elements of list1. Hint: slicing could be helpful here
JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given two ordered linked lists of integers, merge the two lists into a single LinkedList whose elements are in sorted order. You should create a new LinkedList and add the values from list1 and list2 into the new list in sorted order. Do not modify list1 or list 2. list1 => 1 -> 3 -> 7 -> 8, list2 => 2 -> 5 -> 7...
Questions Write a method that returns the union of two array lists of integers using the following header: public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) Write a test program that: 1. prompts the user to enter two lists, each with five integers, 2. displays their union numbers separated by exactly one space., and 3. finds the maximum number and the minimum number in the combined list. Here is a sample run of the program. Enter five integers for list1: 5...
Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41,...
I
need help with this problem this is what I put and these are the
errors I got
Given the lists list1 and list2 that are of the same length, create a new list consisting of the last element of listi followed by the last element of list2, followed by the second to last element of listi, followed by the second to last element of list2, and so on (in other words the new list should consist of alternating elements...
Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...
PYTHON 3 HELP Write a function find_common_elements(list_p,list_q) that accepts two lists, list_p and list_q, as parameters and returns a new list that contains only the elements that are common between two lists (without duplicates). Make sure your program works on two lists of different sizes. Demonstrate your function works by calling it with 2 different pairs of lists. Pseudocode for find_common_elements(list_p,list_q): INITIALIZE empty common_list FOR EACH element e in list_p: IF e is in list_q: ADD e to common_list RETURN...
Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column...
1. Define a function in python that returns the sum of the following 4 lists. Remember to store and re- use your code instead of summing the values for each list 4 times. Hence, define a function that takes as an argument a list and returns the sum of values. Copy the following lists and paste them in a Python file after you define the sum_of_values function Call the function and pass to it a different list each call as...