Write a static method named mergeLists that takes two ArrayLists of Integers as
ID: 3567628 • Letter: W
Question
- Write a static method named mergeLists that takes two ArrayLists of Integers as parameters and returns a new ArrayList of Integers that contain the elements of the original lists "merged" together. By "merged" we mean interleaved going back and forth between the two input lists. For example, if the first list is [4, 7, 1] and the second list is [3, 9, 8], then the new list should be [4, 3, 7, 9, 1, 8]. If one list is longer than the other, append the remaining elements to the end of the new list.
- Write a static method named mergeSortedLists that takes two ArrayLists of Integers that are in increasing order and returns a new ArrayList of Integers that contains the elements of the original lists "merged" together in increasing order. For example, if the first list contains the elements [1, 4, 4, 7, 22] and the second list is [3, 3, 5, 5, 21, 25, 30] then the new list should be [1, 3, 3, 4, 4, 5, 5, 7, 21, 22, 25, 30]. Again, if one list is longer than the other append the remaining elements to theend of the new list.
- Write a statement that declares a Scanner object and initializes it to get input from a file named "input.txt".
- Write a statement that declares a PrintWriter object an initializes it to write output to a file named "output.txt".
- What does the following code output to the screen?
System.out.printf("%,6.2f %-9.9s",1000.2678,"The quick brown fox jumped"); System.out.println(); System.out.printf("%-9s $%,6.2f","The quick brown fox jumped",100.2678); System.out.println(); System.out.printf("%9s $%3.2f","The",100.2678); System.out.println();
Explanation / Answer
public class HelloWorld
{
public static void main(String[] args)
{
int[] ar1={1,2,3};
int[] ar2={4,5,6};
int[] temp = new int[300];
int a1=ar1.length;
int a2=ar2.length;
int j=0,k=0;
for(int i=0;i<a1+a2;i++)
{
if(i%2==0)
{temp[i]=ar1[j];
j++;
}
else
{ temp[i]=ar2[k];
k++;
}
}
for(int i=0;i<a1+a2;i++)
{
System.out.println(temp[i]);
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.