Question: /* * * Append one array list onto the end of another * e.g. if the first array list contains 17 63 41 29 and
/*
*
* Append one array list onto the end of another
* e.g. if the first array list contains 17 63 41 29 and the second contains 23 99 then
* after the append, the first array list should contain 17 63 41 29 23 99
* Difficulty: Easy
*/
import java.util.ArrayList;
public class AppendArrayLists
{
public static void main(String[] args)
{
ArrayList a = new ArrayList();
ArrayList b = new ArrayList();
int i;
for (i = 0; i < 5; i++)
{
/* Initialize array list a to some values */
a.add(i + 3);
}
System.out.print("a: ");
for (i = 0; i < a.size(); i++)
{
System.out.print(a.get(i) + " ");
}
System.out.println();
for (i = 0; i < 3; i++)
{
/* Initialize array list b to some values */
b.add(i + 2);
}
System.out.print("b: ");
for (i = 0; i < b.size(); i++)
{
System.out.print(b.get(i) + " ");
}
System.out.println();
// Append b to the end of a
append(a, b);
System. out.println("Result of append of a and b is: ");
for (i = 0; i < a.size(); i++)
{
System.out.print(a.get(i) + " ");
}
System.out.println();
System.out.println("Expected:3 4 5 6 7 2 3 4");
a.clear();
for (i = 0; i < 4; i++)
{
/* Initialize array list a to some values */
a.add(i + 7);
}
System.out.print("a: ");
for (i = 0; i < a.size(); i++)
{
System.out.print(a.get(i) + " ");
}
System.out.println();
b.clear();
System.out.print("b: ");
for (i = 0; i < b.size(); i++)
{
System.out.print(b.get(i) + " ");
}
System.out.println();
// Append b to the end of a
append(a, b);
System. out.println("Result of append of a and b is: ");
for (i = 0; i < a.size(); i++)
{
System.out.print(a.get(i) + " ");
}
System.out.println();
System.out.println("Expected:7 8 9 10");
}
/**
Append the given ArrayList b to the end of the given ArrayList a
@param integer ArrayList a
@param integer ArrayList b
*/
public static void append(ArrayList a, ArrayList b)
{
//-----------Start below here. To do: approximate lines of code = 3
// append the integer elements in ArrayList b to the end of ArrayList a
// Hint: use a for loop that goes through each element of ArrayList b
// Hint: google "java ArrayList" to see all the methods you can use. Use the add() method
// Hint: the size() method tells you how many elements are stored in the array list
//-----------------End here. Reminder: no changes outside the todo regions.
}
}
Include screenshots too for formatting.
Step by Step Solution
3.43 Rating (153 Votes )
There are 3 Steps involved in it
Append one array list onto the end of another eg if the first array list contains 17 63 41 29 and th... View full answer
Get step-by-step solutions from verified subject matter experts
