Ints toArray() function in Java


The toArray() method of the Ints class returns an array containing each value of collection, converted to a int value in the manner of Number.intValue(). Following is the syntax −

public static int[]
   toArray(Collection<? extends Number> collection)

Here, the parameter is the collection of Number instances

Let us first see an example −

Example

import java.util.Arrays;
import java.util.List;
import com.google.common.primitives.Ints;
class Demo {
   public static void main(String[] args) {
      List<Integer> list = Arrays.asList(20, 40, 60, 80, 100, 120, 140, 160);
      System.out.println("List = "+list);
      int[] myArr = Ints.toArray(list);
      System.out.println("Array (from list) = " + Arrays.toString(myArr));
   }
}

Output

List = [20, 40, 60, 80, 100, 120, 140, 160]
Array = [20, 40, 60, 80, 100, 120, 140, 160]

Updated on: 24-Sep-2019

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements