java 将字符串转换为 int 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36345440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a string into an int array
提问by Derek Yee
I was wondering how would I convert a stringto an intarray.
我想知道如何将 a 转换string为int数组。
I created a reader to read from a text file, which consists of two lines.
我创建了一个读取器来读取文本文件,该文件由两行组成。
- The first line is a random quote like "dogs are red"
- The second line are numbers.
- 第一行是随机引用,如“dogs are red”
- 第二行是数字。
I am supposed to save each character from the quote into a chararray, and then the second line, consisting of the numbers, is the order number of each char, and when printed out in that order, it will spell out a phrase.
我应该将引号中的每个字符保存到一个char数组中,然后由数字组成的第二行是每个的顺序号char,当按照该顺序打印出来时,它会拼出一个短语。
I used the .hasNextLinemethod to first save the two lines into two string variables. Then, I converted the first line into the chararray, and that part is ok but I do not know how to convert the second part into an intarray.
我使用该.hasNextLine方法首先将两行保存为两个字符串变量。然后,我将第一行转换为char数组,那部分没问题,但我不知道如何将第二部分转换为int数组。
I called the variable holding the second line "numbers" and it contains the string "6 25 11 32 6 11 44......"
我将保存第二行的变量称为“数字”,它包含字符串 "6 25 11 32 6 11 44......"
How would I convert this into an intarray?
我将如何将其转换为int数组?
Thanks so much
非常感谢
回答by Magnus W
You could do something real simple like this:
你可以做一些真正简单的事情:
String[] strArray = input.split(" ");
int[] intArray = new int[strArray.length];
for(int i = 0; i < strArray.length; i++) {
intArray[i] = Integer.parseInt(strArray[i]);
}
Also, you'd want to check for NumberFormatExceptionusing a try..catchblock.
此外,您还想检查是否NumberFormatException使用了try..catch块。
回答by robotlos
If your numbers are delimited by spaces like it is described in your post, you can use String.split()to your advantage.
如果您的数字以空格分隔,就像您在帖子中描述的那样,您可以利用String.split()自己的优势。
You can create a method like the following which will convert a Stringof integers to an int[]:
您可以创建一个如下所示的方法,它将String整数a 转换为 an int[]:
public static int[] stringArrayToIntArray(String intString) {
String[] intStringSplit = intString.split(" "); //Split by spaces
int[] result = new int[intStringSplit.length]; //Used to store our ints
for (int i = 0; i < intStringSplit.length; i++) {
//parse and store each value into int[] to be returned
result[i] = Integer.parseInt(intStringSplit[i]);
}
return result;
}
Which can be called like so:
可以这样调用:
public static void main(String[] args) {
String intString = "6 25 11 32 6 11 44"; //Original String
int[] intArray = stringArrayToIntArray(intString); //Call our method
}
If you were to iterate through intArrayand print each value with the following:
如果您要迭代intArray并使用以下内容打印每个值:
for (int i : intArray) {
System.out.println(i);
}
You'd get a result of:
你会得到以下结果:
run:
6
25
11
32
6
11
44
BUILD SUCCESSFUL (total time: 0 seconds)
运行:
6
25
11
32
6
11
44
BUILD SUCCESSFUL(总时间:0 秒)
Cheers!
干杯!

