Java 将整数转换为数字数组
我尝试将整数转换为数组。例如,1234 toJava 将整数转换为数字数组,java,integer,Java,Integer,我尝试将整数转换为数组。例如,1234 toint[]arr={1,2,3,4} 我编写了一个函数: public static void convertInt2Array(int guess) { String temp = Integer.toString(guess); String temp2; int temp3; int [] newGuess = new int[temp.length()]; for(int i=0; i<=temp
int[]arr={1,2,3,4}代码>
我编写了一个函数:
public static void convertInt2Array(int guess) {
String temp = Integer.toString(guess);
String temp2;
int temp3;
int [] newGuess = new int[temp.length()];
for(int i=0; i<=temp.length(); i++) {
if (i!=temp.length()) {
temp2 = temp.substring(i, i+1);
} else {
temp2 = temp.substring(i);
//System.out.println(i);
}
temp3 = Integer.parseInt(temp2);
newGuess[i] = temp3;
}
for(int i=0; i<=newGuess.length; i++) {
System.out.println(newGuess[i]);
}
}
public int[] convertToArray(int number) {
int i = 0;
int length = (int) Math.log10(number);
int divisor = (int) Math.pow(10, length);
int temp[] = new int[length + 1];
while (number != 0) {
temp[i] = number / divisor;
if (i < length) {
++i;
}
number = number % divisor;
if (i != 0) {
divisor = divisor / 10;
}
}
return temp;
}
publicstaticvoidconvertin2array(int-guess){
字符串温度=整数。toString(猜测);
字符串temp2;
int temp3;
int[]newGuess=newint[temp.length()];
for(int i=0;itemp2=temp.substring(i);
将始终返回空字符串“”
相反,您的循环应该具有条件i,您不需要将int
转换为String
。只需使用%10
获取最后一个数字,然后将int除以10即可获得下一个数字
int temp = test;
ArrayList<Integer> array = new ArrayList<Integer>();
do{
array.add(temp % 10);
temp /= 10;
} while (temp > 0);
int-temp=测试;
ArrayList数组=新的ArrayList();
做{
数组。添加(临时%10);
温度/=10;
}而(温度>0);
这将使您的ArrayList以相反的顺序包含您的数字。如果需要,您可以轻松地将其还原,并将其转换为int[]。使用此方法会简单得多:
直接的问题是由于您使用而导致您不必使用子字符串(…)
。使用临时字符(i)
获取数字,并使用以下代码将字符
转换为int
char c = '7';
int i = c - '0';
System.out.println(i);
publicstaticvoidmain(字符串k[])
{
System.out.println(“值的数量=”+k.length);
int arrymy[]=新的int[k.length];
for(int i=0;i
使用:
publicstaticvoidmain(字符串[]args)
{
int num=1234567;
int[]digits=Integer.toString(num.chars().map(c->c-'0').toArray();
for(int d:数字)
系统输出打印(d);
}
主要思想是
将int转换为其字符串值
Integer.toString(num);
获取一个int流,它表示构成整数字符串版本的每个字符(~位)的ASCII值
Integer.toString(num.chars();
将每个字符的ASCII值转换为其值。
要获得字符的实际int值,我们必须从实际字符的ASCII码中减去字符“0”的ASCII码值。
要获取数字的所有数字,必须对构成与数字相等的字符串的每个字符(对应于数字)应用此操作,这是通过将下面的map函数应用于IntStream来完成的
Integer.toString(num.chars().map(c->c-'0');
使用toArray()将int流转换为int数组
Integer.toString(num.chars().map(c->c-'0').toArray();
调用此函数:
public static void convertInt2Array(int guess) {
String temp = Integer.toString(guess);
String temp2;
int temp3;
int [] newGuess = new int[temp.length()];
for(int i=0; i<=temp.length(); i++) {
if (i!=temp.length()) {
temp2 = temp.substring(i, i+1);
} else {
temp2 = temp.substring(i);
//System.out.println(i);
}
temp3 = Integer.parseInt(temp2);
newGuess[i] = temp3;
}
for(int i=0; i<=newGuess.length; i++) {
System.out.println(newGuess[i]);
}
}
public int[] convertToArray(int number) {
int i = 0;
int length = (int) Math.log10(number);
int divisor = (int) Math.pow(10, length);
int temp[] = new int[length + 1];
while (number != 0) {
temp[i] = number / divisor;
if (i < length) {
++i;
}
number = number % divisor;
if (i != 0) {
divisor = divisor / 10;
}
}
return temp;
}
public int[]convertToArray(整数){
int i=0;
int length=(int)Math.log10(number);
整数除数=(int)Math.pow(10,长度);
int temp[]=新的int[length+1];
while(数字!=0){
温度[i]=数/除数;
如果(i<长度){
++一,;
}
数字=数字%除数;
如果(i!=0){
除数=除数/10;
}
}
返回温度;
}
我可以建议以下方法:
将数字转换为字符串→ 将字符串转换为字符数组→ 将字符数组转换为整数数组
下面是我的代码:
public class test {
public static void main(String[] args) {
int num1 = 123456; // Example 1
int num2 = 89786775; // Example 2
String str1 = Integer.toString(num1); // Converts num1 into String
String str2 = Integer.toString(num2); // Converts num2 into String
char[] ch1 = str1.toCharArray(); // Gets str1 into an array of char
char[] ch2 = str2.toCharArray(); // Gets str2 into an array of char
int[] t1 = new int[ch1.length]; // Defines t1 for bringing ch1 into it
int[] t2 = new int[ch2.length]; // Defines t2 for bringing ch2 into it
for(int i=0;i<ch1.length;i++) // Watch the ASCII table
t1[i]= (int) ch1[i]-48; // ch1[i] is 48 units more than what we want
for(int i=0;i<ch2.length;i++) // Watch the ASCII table
t2[i]= (int) ch2[i]-48; // ch2[i] is 48 units more than what we want
}
}
公共类测试{
公共静态void main(字符串[]args){
int num1=123456;//示例1
int num2=89786775;//示例2
字符串str1=Integer.toString(num1);//将num1转换为字符串
字符串str2=Integer.toString(num2);//将num2转换为字符串
char[]ch1=str1.toCharArray();//将str1放入char数组中
char[]ch2=str2.toCharArray();//将str2放入char数组中
int[]t1=new int[ch1.length];//定义t1以将ch1引入其中
int[]t2=new int[ch2.length];//定义t2以将ch2引入其中
对于Scala中的(int i=0;i,可以按如下方式执行:
def convert(a: Int, acc: List[Int] = Nil): List[Int] =
if (a > 0) convert(a / 10, a % 10 +: acc) else acc
在一行中且不颠倒顺序。使用:
int count = 0;
String newString = n + "";
char [] stringArray = newString.toCharArray();
int [] intArray = new int[stringArray.length];
for (char i : stringArray) {
int m = Character.getNumericValue(i);
intArray[count] = m;
count += 1;
}
return intArray;
你必须把它放到一个方法中。我不能给它添加注释,但我认为当你的初始数字可能也低于10时,这也更有效
这是我的建议:
int temp = test;
ArrayList<Integer> array = new ArrayList<Integer>();
do{
array.add(temp % 10);
temp /= 10;
} while (temp > 1);
int-temp=测试;
ArrayList数组=新的ArrayList();
做{
数组。添加(临时%10);
温度/=10;
}而(温度>1);
请记住反转阵列。您可以使用:
String temp = Integer.toString(guess);
int[] newGuess = new int[temp.length()];
for (int i = 0; i < temp.length(); i++)
{
newGuess[i] = temp.charAt(i) - '0';
}
private int[] createArrayFromNumber(int number) {
String str = (new Integer(number)).toString();
char[] chArr = str.toCharArray();
int[] arr = new int[chArr.length];
for (int i = 0; i< chArr.length; i++) {
arr[i] = Character.getNumericValue(chArr[i]);
}
return arr;
}
private int[]createArrayFromNumber(整数){
字符串str=(新整数(数字)).toString();
char[]chArr=str.toCharArray();
int[]arr=新int[chArr.length];
for(int i=0;i
您只需执行以下操作:
char[]digits=string.tocharray();
然后你可以把字符计算成整数
例如:
char[] digits = "12345".toCharArray();
int digit = Character.getNumericValue(digits[0]);
System.out.println(digit); // Prints 1
下面是一个函数,它接受一个整数并返回一个数字数组
static int[] Int_to_array(int n)
{
int j = 0;
int len = Integer.toString(n).length();
int[] arr = new int[len];
while(n!=0)
{
arr[len-j-1] = n % 10;
n = n / 10;
j++;
}
return arr;
}
首先将用户的输入作为int,将其转换为String
,并创建一个大小为str.length()
的字符数组。现在使用charAt()
用for循环填充字符数组
Scanner sc=新扫描仪(System.in);
int num=sc.nextInt();
字符串str=Integer.toString(num);
char[]ch=新字符[str.length()];
对于(int i=0;i您可以这样做:
public int[] convertDigitsToArray(int n) {
int [] temp = new int[String.valueOf(n).length()]; // Calculate the length of digits
int i = String.valueOf(n).length()-1 ; // Initialize the value to the last index
do {
temp[i] = n % 10;
n = n / 10;
i--;
} while(n>0);
return temp;
}
这也将维持秩序。我无法向添加注释,但您可以立即按照正确的方向部署阵列。以下是我的解决方案:
public static int[] splitAnIntegerIntoAnArrayOfNumbers (int a) {
int temp = a;
ArrayList<Integer> array = new ArrayList<Integer>();
do{
array.add(temp % 10);
temp /= 10;
} while (temp > 0);
int[] arrayOfNumbers = new int[array.size()];
for(int i = 0, j = array.size()-1; i < array.size(); i++,j--)
arrayOfNumbers [j] = array.get(i);
return arrayOfNumbers;
}
public static int[]将整数拆分为整数(int a){
内部温度=a;
ArrayList数组=新的ArrayList();
做{
数组。添加(临时%10);
温度/=10;
}而(温度>0);
int[]arrayOfNumbers=新的int[array.size()];
对于(int i=0,j=array.size()-1;ipublic static int[] splitAnIntegerIntoAnArrayOfNumbers (int a) {
int temp = a;
ArrayList<Integer> array = new ArrayList<Integer>();
do{
array.add(temp % 10);
temp /= 10;
} while (temp > 0);
int[] arrayOfNumbers = new int[array.size()];
for(int i = 0, j = array.size()-1; i < array.size(); i++,j--)
arrayOfNumbers [j] = array.get(i);
return arrayOfNumbers;
}
int num = 1234;
String s = Integer.toString(num);
int[] intArray = new int[s.length()];
for(int i=0; i<s.length(); i++){
intArray[i] = Character.getNumericValue(s.charAt(i));
}
ArrayList<Integer> al = new ArrayList<>();
void intToArray(int num){
if( num != 0){
int temp = num %10;
num /= 10;
intToArray(num);
al.add(temp);
}
}
temp - 5 | num - 1234
temp - 4 | num - 123
temp - 3 | num - 12
temp - 2 | num - 1
temp - 1 | num - 0
ArrayList - 1
ArrayList - 1,2
ArrayList - 1,2,3
ArrayList - 1,2,3,4
ArrayList - 1,2,3,4,5
public static void main(String[] args) {
int number = -1203;
boolean isNegative = false;
String temp = Integer.toString(number);
if(temp.charAt(0)== '-') {
isNegative = true;
}
int len = temp.length();
if(isNegative) {
len = len - 1;
}
int[] myArr = new int[len];
for (int i = 0; i < len; i++) {
if (isNegative) {
myArr[i] = temp.charAt(i + 1) - '0';
}
if(!isNegative) {
myArr[i] = temp.charAt(i) - '0';
}
}
if (isNegative) {
for (int i = 0; i < len; i++) {
myArr[i] = myArr[i] * (-1);
}
}
for (int k : myArr) {
System.out.println(k);
}
}
-1
-2
0
-3
Array.from(String(12345), Number);
// Actual number
int n = 56715380;
// Copy of the number
int m = n;
// Find no. of digits as length
int ln = 0;
while (m > 0) {
m = m / 10;
ln++;
}
// Copy of the length
int len = ln;
// Reverse the number
int revNum = 0;
ln--;
int base;
while (n > 0) {
base = 1;
for (int i = 0; i < ln; i++) {
base = base * 10;
}
revNum = revNum + base * (n % 10);
n = n / 10;
ln--;
}
// Store the reverse number in the array
int arr[] = new int[len];
for (int i = 0; revNum > 0; i++) {
arr[i] = revNum % 10;
revNum = revNum / 10;
}
// Print the array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}