Activity 4.33.1.
Fix the following code so that it compiles. The code should instantiate an ArrayList of Strings
names
and fill it with the Strings from the array friends
. It should then print out names
.
Solution.
In line 10, change the terminating condition to
i <
friends.length
so that you donβt go out of bounds of the array.
import java.util.*;
public class Test1
{
public static void main(String[] args)
{
ArrayList<String> names = new ArrayList<String>();
String[] friends = {"Sam", "Jessica", "Mark", "Alexis"};
for (int i = 0; i < friends.length; i++)
{
names.add(friends[i]);
}
System.out.println(names);
}
}