1. import java.util.*;
2. class MyList {
3. public static void main(
String [] args) {
4. LinkedList<String> list = new LinkedList<String>();
5. list.add("one "); list.add("two "); list.add("three ");
6. String [] sa = new String[3];
7. // insert code here
8. for(String s : sa)
9. System.out.print(s);
10. }
11. }
Which, inserted at line 7, allows the code to compile and run without exception?
A) sa = (String []) list.toArray();
B) sa = (Object []) list.toArray();
C) sa = list.toArray(sa);
Answer C.
Why is A and B wrong. For B, Couldn't the Object[] or String[] cast be used. Is it because using the syntac String [] as a valid cast wrong?
Can I use sa = (String []) list.toArray(new String[0]) or just (String []) list.toArray(new String[0])??