How would i finish this code? Make a copy of an ArrayList with an explicit loop.
ID: 3929297 • Letter: H
Question
How would i finish this code?
Make a copy of an ArrayList with an explicit loop. Complete the following code. Complete the following file: Array Lis tCopy. java import java.util.ArrayList; import java.util.Scanner; public class ArrayListCopy public static void main(String[] args) {Arraylist words = new ArrayList(); Scanner in = new Scanner(System.in); while (in.hasNextO) words.add(in.next()); ArrayList copyOfWords = new ArrayList();//Use a for loop to copy the contents of words ... words.remove(0);//Shouldn't affect the copy System.out.println(words); System.out.println(copyOfWords);Explanation / Answer
ArrayList<String> copyOfWords=new ArrayList<String>();
for(String s:words)
{
copyOfWords.add(s);
}
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.