Convert collection to int array

I want to display a graph and show that I need integer values. I get this from my code

Collection c = Sort.values(); 

Is there a way to convert the collection in such a way that I get integer values? I get it when I print collection c

  [64770, 26529, 13028, 848, 752, 496] 
+8
java collections
source share
3 answers

Assuming the values ​​are of type Integer , you can try the following:

 Collection c = Sort.values(); Integer[] a = (Integer[])(c.toArray(new Integer[c.size()])); 
+15
source share
 for (Integer value : c) { int i = value.intValue(); //do something with either value or i } 
+3
source share

Just:

 Integer[] yourArrayVar = yourCollectionVar.toArray(new Integer[0]); 

java just needs to know what type of array will be created.

+1
source share

All Articles