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 string
to an int
array.
我想知道如何将 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 char
array, 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 .hasNextLine
method to first save the two lines into two string variables. Then, I converted the first line into the char
array, and that part is ok but I do not know how to convert the second part into an int
array.
我使用该.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 int
array?
我将如何将其转换为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 NumberFormatException
using a try..catch
block.
此外,您还想检查是否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 String
of 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 intArray
and 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!
干杯!