1

我有必要将两位十进制数转换为 int 数组。例如,如果我们假设我有32我想将其转换为int[] a = new int[2]whereint[0] = 3和的数字int[1] = 2

4

3 回答 3

1

您可以尝试:

int x = 32;
int[] array = { x / 10, x % 10 };
于 2015-02-05T08:07:31.510 回答
0

通用版本:

假设您的号码存储在变量 x 中。

final int x = 345421;
final String serialized = String.valueOf(x);
final int[] array = new int[serialized.length()];
for (int i = 0; i < serialized.length(); i++) {
    array[i] = Integer.valueOf(String.valueOf(serialized.charAt(i)));
}
于 2015-02-05T08:39:13.217 回答
0

尝试这个:

int x, temp, i=0, j;
int[] a, b;
//Get the integer first and store in x 
//Also allocate the size of a and b with the number of digits that integer has
while(x > 0)
{
  temp = x%10;//gives remainder
  a[i] = temp;//store that value in array
  x = x/10;//to get the remaining digits
}
//the array will have the numbers from last digit

前任:

我有 x = 322

您将数组 a 作为 { 2, 2, 3 }

 //to get the actual array try this
 j = a.length;
 while(i<j)
 {
    b[i]=a[j];
    i++;
    j--;
 }

您将数组 b 作为 { 3, 2, 2 }

于 2015-02-05T08:17:42.477 回答