What is the result of the following statements?
public class Main{ public static void main(String[] argv){ ArrayList<Integer> values = new ArrayList<>(); values.add(4); values.add(5); values.set(1, 6); values.remove(0); for (Integer v : values) System.out.print(v); } }
C.
The code creates an array list and add two values to it. Then the code set the second value to 6 and removed the first element.
import java.util.ArrayList; /* w w w . j a v a 2s . c o m*/ public class Main{ public static void main(String[] argv){ ArrayList<Integer> values = new ArrayList<>(); values.add(4); System.out.print(values); values.add(5); System.out.print(values); values.set(1, 6); System.out.print(values); values.remove(0); System.out.print(values); for (Integer v : values) System.out.print(v); } }
The code above generates the following result.