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

1
  • Do you know about Short.parseShort? Use that in a loop. Commented Oct 15, 2013 at 22:31

2 Answers 2

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.)

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.