英文:
how to convert string to int[] and retrieve the array for further processing
问题
String s = "7979, 333, 222, 9999";
I want to convert the String above to an array:
int[] con = {7979, 333, 222, 999};
and use this array further
Here is my code:
Object[] array = arrayList.toArray();
String sta = (Arrays.toString(array));
String stb = sta.replaceAll("\\[", "").replaceAll("\\]", "");
tv.setText(stb);
System.out.println(stb);
int[] numbers = Arrays.asList(numbersArray.split(","))
.stream()
.map(String::trim)
.mapToInt(Integer::parseInt)
.toArray();
英文:
String s="7979, 333, 222, 9999";
I want to convert the String above to an array:
int[] con = {7979,333,222,999};
and use this array further
Here is my code:
Object[] array = arrayList.toArray();
String sta= (Arrays.toString(array));
String stb= sta.replaceAll("\\[", "").replaceAll("\\]","");
tv.setText(stb);
System.out.println(stb);
int[] numbers = Arrays.asList(numbersArray.split(","))
.stream()
.map(String::trim)
.mapToInt(Integer::parseInt)
.toArray();
答案1
得分: 1
你可以按照以下方式进行操作:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String s = "7979, 333, 222, 9999";
int[] nums = Arrays.stream(s.split("\\s*,\\s*")) // 通过逗号前后的可选空格分割字符串
.mapToInt(e -> Integer.parseInt(e)) // 将每个字符串映射为整数,获取 IntStream
.toArray(); // 将 IntStream 转换为数组
System.out.println(Arrays.toString(nums));
}
}
输出:
[7979, 333, 222, 9999]
英文:
You can do it as follows:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String s = "7979, 333, 222, 9999";
int[] nums = Arrays.stream(s.split("\\s*,\\s*")) // Split the string on comma preceded/followed by optional whitespace(s)
.mapToInt(e -> Integer.parseInt(e)) // Get an IntStream by mapping each string into an integer
.toArray(); // Convert the IntStream to array
System.out.println(Arrays.toString(nums));
}
}
Output:
[7979, 333, 222, 9999]
答案2
得分: 1
你可以使用 .split()
方法拆分字符串,并使用 parseInt
将字符串解析为整数:
int[] val = Arrays.stream(s.split(", ")).mapToInt(Integer::parseInt).toArray();
英文:
You can split the string using .split()
method and parse string into int using parseInt
int[] val = Arrays.stream(s.split(", ")).mapToInt(Integer::parseInt).toArray();
答案3
得分: 1
有多种String类的方法可以实现您想要的效果,其中之一是使用split()
方法。
String s = "7979, 333, 222, 9999";
String[] stringArray = s.split(",");
//或者使用以下代码来去除任何空格
String[] stringArray = s.split("\\s*,\\s*");
int[] con = new int[stringArray.length]; //初始化整数数组
//迭代数组将字符串转换为整数
for (int i = 0; i < con.length; i++) {
con[i] = Integer.parseInt(stringArray[i]);
}
// int数组con中将会包含所需的结果。当然也有其他很多优化的方法可以达到相同的效果。
祝编码愉快!
英文:
There are multiple method of String Class to achieve what you are trying, one of them is to use spit()
method.
String s="7979, 333, 222, 9999";
String [] stringArray = s.split(",");
//or use followig to remove any blank spaces.
String[] stringArray = s.split("\\s*,\\s*");
int []con = new int[stringArray.length()]; //initialize int array
//iterate array to convert string to integer
for(int i=0; i<con.length();i++{
con[i] = Integer.parseInt(stringArray[i]);
}
int []con will have the desired results. There could be many other optimal ways as well to achieve the same.
Happy Coding!
答案4
得分: 0
使用方法split(",")将您的字符串转换为由逗号分隔的字符串数组。
然后,您应该循环遍历您创建的数组,并将Integer.parseint(您的字符串)的结果添加到一个整数列表中。
我不会输入任何代码,因此您可以继续搜索一下。
英文:
Use the method split(",") to convert your string to an array of string separated by the coma value.
Then you should loop into your created array and add into an int list the result of Integer.parseint(your string)
I wont type any code so you keep searching a bit
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论