如何将数字串拆分为int数组?

时间:2022-11-25 21:45:11

How to split string containing numbers into int array i.e

如何将包含数字的字符串拆分为int数组,即

String s="12345";
int i[]=new int[5]; 
i[0]=1; 
i[1]=2;
i[2]=3; 
i[3]=4; 
i[4]=5;

i have tried it by

我试过了

String s="12345";
int i[]=new int[5];
int tem = Integer.parseInt(s);
for(int t=0;t<5;t++){
i[4-t]=tem%10;
tem=tem/10;
}

it is giving right answer in above case but in case of String s="73167176531330624919225119674426574742355349194934" it fails so any other way or how to use split method in above case

它在上面的情况下给出了正确的答案,但是在String s =“73167176531330624919225119674426574742355349194934”的情况下,它失败,所以任何其他方式或如何使用上述情况下的拆分方法

4 个解决方案

#1


6  

You can use Character.getNumericValue(char)

你可以使用Character.getNumericValue(char)

String str = "12345";
int[] nums = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
    nums[i] = Character.getNumericValue(str.charAt(i));
}

#2


2  

That's because your number wont fit in the integer range nor in long range. Also to note, Your code wont be that efficient due to division and modulas operator as well. Instead you could always use charAt api of String and convert individual characters to a number as give below:

那是因为你的数字不适合整数范围,也不适合长距离。另外需要注意的是,由于除法和模块运算符,您的代码也不会那么高效。相反,您可以始终使用String的charAt api并将单个字符转换为下面给出的数字:

String s = "73167176531330624919225119674426574742355349194934";
int[] numbers = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
   numbers[i] = s.charAt(i) - '0';
}
System.out.println(Arrays.toString(numbers));
Output:
[7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9, 1, 9, 2, 2, 5, 1, 1, 9, 6, 7, 4, 4, 2, 6, 5, 7, 4, 7, 4, 2, 3, 5, 5, 3, 4, 9, 1, 9, 4, 9, 3, 4]

#3


0  

The culprit is this line of code:

罪魁祸首就是这行代码:

int tem = Integer.parseInt(s);

When you enter a large number is string which is outside the range of what an int can accomodate, the overflow happens, and thus all of a sudden you are working on a different number than what was in your string.

当你输入一个大数字时,字符串超出了int可以容纳的范围,溢出就会发生,因此突然间你正在使用与字符串中不同的数字。

I would suggest you iterate over each character of the string, and then convert each character to integer:

我建议你迭代字符串的每个字符,然后将每个字符转换为整数:

for (char ch: s.toCharArray()) {
    // convert ch to integer, and add to the array.
    intArray[i] = (int)(ch - '0');
    // of course keep incrementing `i`
}

#4


0  

You can simple use Character wrapper class and get the numeric value and add it to array of in.

您可以简单地使用Character包装器类并获取数值并将其添加到in的数组中。

        String sample = "12345";

        int[] result = new int[sample.length()];

        for(int i=0;i<result.length;i++)
        {
            result[i] = Character.getNumericValue(sample.charAt(i));
        }

#1


6  

You can use Character.getNumericValue(char)

你可以使用Character.getNumericValue(char)

String str = "12345";
int[] nums = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
    nums[i] = Character.getNumericValue(str.charAt(i));
}

#2


2  

That's because your number wont fit in the integer range nor in long range. Also to note, Your code wont be that efficient due to division and modulas operator as well. Instead you could always use charAt api of String and convert individual characters to a number as give below:

那是因为你的数字不适合整数范围,也不适合长距离。另外需要注意的是,由于除法和模块运算符,您的代码也不会那么高效。相反,您可以始终使用String的charAt api并将单个字符转换为下面给出的数字:

String s = "73167176531330624919225119674426574742355349194934";
int[] numbers = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
   numbers[i] = s.charAt(i) - '0';
}
System.out.println(Arrays.toString(numbers));
Output:
[7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9, 1, 9, 2, 2, 5, 1, 1, 9, 6, 7, 4, 4, 2, 6, 5, 7, 4, 7, 4, 2, 3, 5, 5, 3, 4, 9, 1, 9, 4, 9, 3, 4]

#3


0  

The culprit is this line of code:

罪魁祸首就是这行代码:

int tem = Integer.parseInt(s);

When you enter a large number is string which is outside the range of what an int can accomodate, the overflow happens, and thus all of a sudden you are working on a different number than what was in your string.

当你输入一个大数字时,字符串超出了int可以容纳的范围,溢出就会发生,因此突然间你正在使用与字符串中不同的数字。

I would suggest you iterate over each character of the string, and then convert each character to integer:

我建议你迭代字符串的每个字符,然后将每个字符转换为整数:

for (char ch: s.toCharArray()) {
    // convert ch to integer, and add to the array.
    intArray[i] = (int)(ch - '0');
    // of course keep incrementing `i`
}

#4


0  

You can simple use Character wrapper class and get the numeric value and add it to array of in.

您可以简单地使用Character包装器类并获取数值并将其添加到in的数组中。

        String sample = "12345";

        int[] result = new int[sample.length()];

        for(int i=0;i<result.length;i++)
        {
            result[i] = Character.getNumericValue(sample.charAt(i));
        }