我怎么能解析一个String []到java中的int []? [英] How can I parse a String[] to an int[] in java?
本文介绍了我怎么能解析一个String []到java中的int []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有代码:
String s =a,b,c,d,e
int [] i = s.split(,);
但此强制类型不可用。
有人可以帮助我吗?
感谢
解决方案
您必须循环遍历数组中的每个元素,
像这样:
String s =a,b,c,d, e。
String [] strings = s.split(,);
int [] i = new int [strings.length];
for(int j = 0; j< strings.length; j ++)
{
i [j] = Integer.parseInt(strings [j]);
}
请注意,此代码将崩溃,因为字符串数组中的元素't整数。
I have the code:
String s = "a,b,c,d,e";
int[] i = s.split(",");
but this cast is not avaiable.
Some one can help me? Thanks
解决方案
You must loop over each element in the array and cast them one by one.
Like this:
String s = "a,b,c,d,e";
String[] strings = s.split(",");
int[] i = new int[strings.length];
for(int j = 0; j < strings.length; j++)
{
i[j] = Integer.parseInt(strings[j]);
}
Note that this code will crash, since the elements in the string-array aren't integers.
这篇关于我怎么能解析一个String []到java中的int []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文