[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);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.