Bird
0
0

How can you convert a List<Integer> back to a primitive int[] array?

hard📝 Application Q9 of 15
Java - Wrapper Classes

How can you convert a List<Integer> back to a primitive int[] array?

AUse Arrays.copyOf() on the List
BCast List<Integer> to int[] directly
CUse toArray() method on List<Integer>
DUse stream().mapToInt(Integer::intValue).toArray()
Step-by-Step Solution
Solution:
  1. Step 1: Understand conversion from List to primitive array

    List<Integer> cannot be cast directly to int[].
  2. Step 2: Use Java streams for conversion

    Using stream().mapToInt(Integer::intValue).toArray() converts Integer list to int array efficiently.
  3. Final Answer:

    Use stream().mapToInt(Integer::intValue).toArray() -> Option D
  4. Quick Check:

    Streams convert List to int[] correctly [OK]
Quick Trick: Use streams to convert List to int[] [OK]
Common Mistakes:
  • Trying to cast List to int[]
  • Using toArray() which returns Integer[]
  • Using Arrays.copyOf on List

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes