Considering the restrictions on generics, it is evident that, a Generic Array creation is not allowed. For example, E[] a = new E[10];is invalid. Which of the following statements offers a simple solution and rectifies this problem?Group of answer choicesObject[] a = (E) new Object [10];E[] a = (E) new Object [10];E[] a = (Object) new E[10];E[] a = (E[]) new Object [10];
Question
Considering the restrictions on generics, it is evident that, a Generic Array creation is not allowed. For example, E[] a = new E[10];is invalid. Which of the following statements offers a simple solution and rectifies this problem?Group of answer choicesObject[] a = (E) new Object [10];E[] a = (E) new Object [10];E[] a = (Object) new E[10];E[] a = (E[]) new Object [10];
Solution
The correct statement that offers a simple solution and rectifies the problem of generic array creation is:
E[] a = (E[]) new Object [10];
This statement creates an array of Objects and then casts it to an array of E. This is a workaround for the restriction on generic array creation in Java.
Similar Questions
Which of the following array creation statements are incorrect? Please select all that apply.Group of answer choicesint[] a = new int[2];int a[] = new int[2];int[] a = new int(2);int a = new int[2];int a() = new int[2];
Which of the following is incorrect? Group of answer choicesint[] a = new int[2];All are correctint[][] a = new int[2];int a[] = new int[2];
Which of the following statements is VALID?Group of answer choicesArrayList<? > l = new ArrayList<? extends Number >( );ArrayList<? > l = new ArrayList<?>( );ArrayList<? extends Number> l = new ArrayList<String>( );ArrayList<? super String> l = new ArrayList<Object>( );
How do you declare a 2-dimensional array in Java? Group of answer choicesint[][] array;int array[][];int[] array[];All of the above
Analyze the following code.int[] list = new int[5];list = new int[6];Group of answer choicesThe code can compile and run fine. The second line assigns a new array to list.The code has compile errors because you cannot assign a different size array to list.The code has compile errors because the variable list cannot be changed once it is assigned.The code has runtime errors because the variable list cannot be changed once it is assigned.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.