Here you can find the source of toArray(List
Parameter | Description |
---|---|
l | The list. It may be null, in which case null is returned. |
public static int[] toArray(List<Integer> l)
//package com.java2s; /* $License$ */// www.ja v a 2 s .co m import java.util.List; public class Main { /** * Returns the non-null integers in the given list as an array. * * @param l The list. It may be null, in which case null is * returned. * * @return The array. */ public static int[] toArray(List<Integer> l) { if (l == null) { return null; } int count = 0; for (Integer e : l) { if (e != null) { count++; } } int[] result = new int[count]; int i = 0; for (Integer e : l) { if (e != null) { result[i++] = e; } } return result; } }