Java Make a copy of an ArrayList with an explicit loop The o
[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
Solution
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);
}
![[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 [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](/WebImages/27/java-make-a-copy-of-an-arraylist-with-an-explicit-loop-the-o-1072441-1761561964-0.webp)