Java List to Array toArray(List l)

Here you can find the source of toArray(List l)

Description

Returns the non-null integers in the given list as an array.

License

Open Source License

Parameter

Parameter Description
l The list. It may be null, in which case null is returned.

Return

The array.

Declaration


public static int[] toArray(List<Integer> l) 

Method Source Code


//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;
    }
}

Related

  1. toArray(List list)
  2. toArray(List list)
  3. toArray(List values)
  4. toArray(List enums)
  5. toArray(List from, int[] to)
  6. toArray(List list)
  7. toArray(List list)
  8. toArray(List list)
  9. toLongList(String str, String splitStr)