i have string-array resource integers.
<string-array name="navchildratings"> <item>12</item> <item>12</item> <item>17</item> <item>123</item> <item>8</item> </string-array>
my goal have them list of type list<integer>
first step know can assigned integer array by:
int[] ratings = arrays.aslist(getresources().getintarray(r.array.navchildratings));
i trying avoid looping through array of integers (ints) , having add 1 one list of integers (java.lang.integer).
- is there direct way string-array
list<integer>
?
or, alternatively - is there direct way assign
int[]
arraylist<integer>
?
note: motivation purely have more elegant code. know how looping array. but, example, in case of strings works if assign directly:
list<string> names = arrays.aslist(getresources().getstringarray(r.array.navchildnames));
unofortunately impossible since aslist not handling boxing (wrapping primitive) , not create objects automatically.
if want keep code elegant , using java8 can create lambda one-liner
if not use java8 create simple method convert int[] list
arraylist<integer> getlist(int[] a) { list<integer> l = new arraylist<integer>(); for(int : a) l.add( new integer(i) ); return l; }
and then
list<integer> items = getlist(ratings);
Comments
Post a Comment