0

I have a string-array resource with integers.

<string-array name="navChildRatings">
    <item>12</item>
    <item>12</item>
    <item>17</item>
    <item>123</item>
    <item>8</item>

</string-array>

My goal is to have them into a list of type List<Integer>
As a first step I know they can be assigned into an integer array by:

int[] ratings = Arrays.asList(getResources().getIntArray(R.array.navChildRatings));

I am trying to avoid looping through the array of integers (ints) and having to add one by one to the List of Integers (java.lang.Integer).

  1. Is there a direct way to get the string-array into a List<Integer>?
    or, alternatively
  2. Is there a direct way to assign the int[] array to a List<Integer>?

Note: My motivation is purely to have a more elegant code. I know how to do it by looping the array. But, for example, in the case of Strings it works if you assign it directly:

List<String> names = Arrays.asList(getResources().getStringArray(R.array.navChildNames));
Cœur
  • 37,241
  • 25
  • 195
  • 267
ilomambo
  • 8,290
  • 12
  • 57
  • 106
  • 2
    Shouldn't that be an ``? – George Mulligan Jan 24 '16 at 18:51
  • Maybe you are right, I will test it and let you know. – ilomambo Jan 24 '16 at 18:59
  • It still won't get your code to work the way you want but at least you won't have to parse the strings into Integers. You will have to use a for loop if you need a `List` from the int[]. To keep it elegant you can write your own helper method that takes an int[] and returns a `List` so you only have to write the loop once. – George Mulligan Jan 24 '16 at 19:01
  • Duplicate of: http://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java – Marco Altieri Jan 24 '16 at 19:02
  • @MarcoAltieri Not a duplicate. I am trying to avoid looping, which is not addressed in that question. – ilomambo Jan 24 '16 at 19:04
  • Ok. I meant that there is already an answer that says that is not possible – Marco Altieri Jan 24 '16 at 19:07
  • @GeorgeMulligan You were right on both your comments, thanks. Still I will have to do that helper thing. – ilomambo Jan 24 '16 at 19:10
  • Is there a reason you need a `List` instead of an `int[]`? I ask because if the default `List` implementation returned by `asList()` is in fact an `ArrayList`, then you aren't actually gaining anything (since `ArrayList` is backed by an array anyway). In fact, you may lose some performance/memory because all your ints now have to be boxed. – Karakuri Jan 24 '16 at 19:39
  • @Karakuri I need the List<> to pass to on to a library method that requires List – ilomambo Jan 25 '16 at 08:19

1 Answers1

1

Unofortunately this is impossible since asList is not handling boxing (wrapping primitive) and will not create objects automatically.

If you want to keep code elegant and using Java8 you can easily create lambda to do it as one-liner

if you do not use Java8 just create a simple method to convert int[] to List

    ArrayList<Integer> getList(int[] a)
    {
        List<Integer> l = new ArrayList<Integer>();

        for(int i : a)
            l.add( new Integer(i) );

        return l;
    }

and then

    List<Integer> items = getList(ratings);
m.antkowicz
  • 13,268
  • 18
  • 37