0

hello I have a commaseparated list of string and put into an array.

I ultimately need them as a list of shorts, but the only way I know how to do that is get them as an array of shorts then do array.asList()

String[] stringArray= commaSeparatedString.split("\\s*,\\s*");

How can I get that to an array of shorts so I can throw in a list?

THanks

Doc Holiday
  • 9,928
  • 32
  • 98
  • 151

2 Answers2

5

Well presumably you just need to parse each element. But why not just add them to an ArrayList<Short>?

List<Short> shortList = new ArrayList<Short>(stringArray.length);
for (int i = 0; i < stringArray.length; i++) {
    shortList.add(Short.valueOf(stringArray[i]));
}

(Note that you can't have a list of a primitive type, as Java generics don't support primitive type arguments.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

After the splitting you parse each string to the desired type for example Short.parseShort(element)

Thomas
  • 11,272
  • 2
  • 24
  • 40