1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Kryger [21]
4 years ago
7

Write a program called interleave that accepts two ArrayLists of integers list1 and list2 as parameters and inserts the elements

of list2 into list1 at alternating indexes. If the lists are of unequal length, the remaining elements of the longer list are left at the end of list1.
Computers and Technology
1 answer:
nata0808 [166]4 years ago
7 0

Answer:

Explanation code is given below along with step by step comments!

Explanation:

// first we create a function which accepts two ArrayList of integers named list1 and list2

public static void interleave(ArrayList<Integer> list1, ArrayList<Integer> list2)

{

// we compare the size of list1 and list2 to get the minimum of two and store it in variable n

   int n = Math.min(list1.size(), list2.size());

   int i;

// here we are getting the elements from list2 n times then we add those elements in the list1 alternating (2*i+1)

   for (i = 0; i < n; i++)

     {

       int x = list2.get(i);

       list1.add(2 * i + 1, x);

      }

// if the size of list1 and list2 is same then program stops here else we need to append extra elements at the end of list1

// then we check if the size of list2 is greater than list1 then simply add the remaining elements into list1

   if (i < list2.size())

{

       for (int j = i; j < list2.size(); j++)

           {

                list1.add(list2.get(j));

            }  

     }  

}

Sample Output:

list1=[1, 2, 3]

list2=[5, 6, 7, 8, 9]

list1=[1, 5, 2, 6, 3, 7, 8, 9]

You might be interested in
A good way to assess your CPU usage is to:_______.
inna [77]
C is likely the best answer here.
8 0
3 years ago
Who is the best basketball player ever... if you do not say lebron do not comment
Andrews [41]

Kobe was the best basketball player R.I.P

6 0
3 years ago
What is “GoF” and how does it relate to design patterns?
tatuchka [14]

Answer:

GOF refers to the gang of four pattern that are generally consider the basic for all the other patterns. Design pattern are basically provide the solution to the software design to resolve all problems that are associated with the development of real world applications.

GOF Design pattern implemented the parts of the re-usable object oriented software applications. The main aim of design pattern is to pass all the structural design pattern. Design pattern is the most powerful and helpful tool for the software developer and architecture.  

3 0
3 years ago
Because some countries have poor traditional telephone services, companies and consumers have resorted to Group of answer choice
maw [93]

Answer:

a. leap frogging

Question:

Because some countries have poor traditional telephone services, companies and consumers have resorted to Group of answer choices leap frogging. express package services. satellite telephones. fiber-optic telephones. voice over internet protocol (VOIP) services.

3 0
3 years ago
Which of the following statements is not correct ​
just olya [345]
Wich statements ? I don’t see anything
4 0
3 years ago
Read 2 more answers
Other questions:
  • Can i earn money at weegy.com ?
    10·1 answer
  • Why you should care about copyright
    13·1 answer
  • #11. Write a program that prompts the user to input a four-digit positive integer. The program then outputs the digits of the nu
    6·1 answer
  • Enter a word: Good<br> Enter a word: morning<br> Good morning
    8·2 answers
  • What kind of app or technology would you like to create?  Why ? <br><br><br>​
    11·1 answer
  • Help!!!!!!!!!!!!!!!!!
    11·1 answer
  • Which of the following sentences is accurate? a. PDF stands for "Proofreading Direct Files." b. PDF files cannot be edited. c. B
    9·1 answer
  • If I write too much for an exam answer, do I get downgraded?
    7·1 answer
  • 1.a computer can create an output based on the input of the user.
    13·1 answer
  • Explain how loops can be utilized in list processing. Please provide Python examples in your response.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!