如何转换为字符串,如:如何将字符串数组转换为int数组?
"114 214 219"
成一个int数组,所以我有这样的事情存储在我的int数组:
int[] couseNumbers = int[3];
for(int i = 0; i < 3; i++){
courseNumbers[i] = Integer.parseInt(/*get int in the string array*/);
}
如何转换为字符串,如:如何将字符串数组转换为int数组?
"114 214 219"
成一个int数组,所以我有这样的事情存储在我的int数组:
int[] couseNumbers = int[3];
for(int i = 0; i < 3; i++){
courseNumbers[i] = Integer.parseInt(/*get int in the string array*/);
}
首先将字符串拆分为数组。
String[] stringArray = "114 214 219".split(" ");
然后在循环中,您可以访问阵列:
courseNumbers[i] = Integer.parseInt(stringArray[i])
非常感谢! –
如果它帮助你,请将我的答案标记为解答;) – Markus
使用Java 8流。
List<Integer> courseNumbers = Arrays.stream("1123 213 23"
.split(" ")).map(x-> Integer.parseInt(x))
.collect(Collectors.toList());
谢谢,但它会更好的数组。 –
试试这个
String s = "1 2 3";
String array[] = s.split(" "); //Splits the Array by spaces
int a[] = new int[array.length]; //Makes new int array
for(int i=0;i<a.length;i++){
a[i] = Integer.parseInt(array[i]); //Converts String to int
}
感谢您的帮助! –
回答上述由爱德华丹尼斯不会为字符串工作不能转化为直接诠释。你可以做这样的事情。
public static void main(String []args){
String s = "114 214 219";
String [] courseNumbers = s.split(" ");
Integer [] intArray = new Integer[courseNumbers.length];
for(int i= 0; i < courseNumbers.length; i++){
intArray[i] = Integer.parseInt(courseNumbers[i]);
}
}
非常感谢! –
我不明白你的问题是什么?你拥有的代码应该工作得很好。 – Jdman1699
那么,除了'couseNumbers'和'courseNumbers'是不同的名字...... –