JAVA: Provide a static method void append(ArrayList<T> a, ArrayList<T> b) that a
ID: 663700 • Letter: J
Question
JAVA: Provide a static method void append(ArrayList<T> a, ArrayList<T> b) that appends the elements of b to a.
HELP!
This is what I have:
import java.util.ArrayList;
public class ArrayListA
{
ArrayList<Sting> a = new ArrayList <string>();
a.add("apple");.add("banana");
b.add("carrot");
b.add("zucchini");
System.out.print("A Contents" + printMyList(a));
append(a, b);
System.out.println("A append" + printMyList(a));
}
public static printMyList(Array<String> myList)
{
String str = "";
for (String c : myList)
{ str = str + c + "";}
str = str + " ";
return str;
}
}
public static <T> void append(ArrayList<T> f, ArrayList<T> g)
{
for (T temp = g)
{ f.add(temp); }
}
Explanation / Answer
I am using two approaches here one with Iterator and one without Iterators
With Iterator
Without Iterator
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.