Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[Java] Make a copy of an ArrayList<String> with an explicit loop. The output sho

ID: 3929393 • Letter: #

Question

[Java]

Make a copy of an ArrayList<String> with an explicit loop.

The output should look like this:

or

I couldn't get the last line.(Bold parts)

Here's my code:

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListCopy
{
public static void main(String[] args)
{
ArrayList<String> words = new ArrayList<String>();
Scanner in = new Scanner(System.in);
while (in.hasNext()) words.add(in.next());

ArrayList<String> copyOfWords = new ArrayList<String>();

for(int i = 0; i >= words.size(); i++)
{
copyOfWords.add(i, words.get(i));
}

words.remove(0);
System.out.println(words);
System.out.println(copyOfWords);
}
}

Fix bold part usuing copyOfWords.add method please

Explanation / Answer

Try using this: Condition in for loop is changed


   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
ArrayList<String> words = new ArrayList<String>();
Scanner in = new Scanner(System.in);
while (in.hasNext()) words.add(in.next());
ArrayList<String> copyOfWords = new ArrayList<String>();
System.out.println(words.size());
for(int i = 0; i <words.size(); i++)
{
copyOfWords.add(i, words.get(i));
}
words.remove(0);
System.out.println(words);
System.out.println(copyOfWords);
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote