java convert string to int array

www.laut‮t‬uri.com
java convert string to int array

To convert a String containing a list of integer values separated by a delimiter to an int array in Java, you can use the split method of the String class to split the String into an array of Strings, and then use the Integer.parseInt method to parse each String element as an int value and add it to the int array.

Here is an example of how you can convert a String to an int array in Java:

String str = "1,2,3,4,5";
String[] elements = str.split(",");
int[] values = new int[elements.length];
for (int i = 0; i < elements.length; i++) {
    values[i] = Integer.parseInt(elements[i]);
}

In this example, we have a String called str that contains a list of integer values separated by commas. We use the split method to split the String into an array of Strings, and assign it to a String array called elements. We then create an int array called values with the same length as the elements array, and use a loop to iterate over the elements array. For each element, we use the Integer.parseInt method to parse the String element as an int value, and add it to the values array.

Created Time:2017-11-03 00:14:52  Author:lautturi