Question 6
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
class Buffer<T> {
private final T[] slots = new T[10];
T get(int i) { return slots[i]; }
}
public class Main {
public static void main(String[] args) {
System.out.println(new Buffer<String>().get(0));
}
}
- A 10 is too small
- B Arrays need a known run-time element type, but T is erased; use a List<T>, or (T[]) new Object[10] with care Correct
- C get must be static
- D slots must not be final
Correct Answer: Option B
javac: "generic array creation".
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!