user40380
user40380

Reputation: 76

List of Strings to List of Integers?

I was wondering if the following code converts a list with string values to a list of integer values?

Example of what I want : ['14','15','67'] ---> [14,15,67]

    String s = strLine;
    List myList = new ArrayList<String>(Arrays.asList(s.split(" ")));
    List<Integer> intList =  myList;

Or do I need to iterate over it using a for loop?

Upvotes: 0

Views: 89

Answers (1)

Farmer Joe
Farmer Joe

Reputation: 6070

Java keeps a tight ship. You need to try and parse the Strings into integers:

List<Integer> intList = new List<Integer>();
for (String s : strList) {
    intList.add(Integer.parseInt(s));
}

Upvotes: 3

Related Questions