英文:
How do I convert an Int to an Int[] in java?
问题
我想将一个原始的Java整数值转换为整数数组:
int myInt = 4821;
转换为整数数组:
int[] myInt = {4, 8, 2, 1};
英文:
I want to convert a primitive Java integer value:
int myInt = 4821;
To an integer array:
int[] myInt = {4, 8, 2, 1};
答案1
得分: 3
import java.util.stream.Stream;
public static int[] convertValueToIntegerArray(int value) {
return Stream.of(String.valueOf(value).split(""))
.mapToInt(Integer::parseInt)
.toArray();
}
import java.util.Arrays;
public void testIntegerArray() {
int myInt = 4821;
int[] myIntArr = convertValueToIntegerArray(myInt);
System.out.println("Converted: " + Arrays.toString(myIntArr));
}
英文:
Following code for a simple one line solution using Java 8+ with Stream
and mapToInt
:
Stream.of(String.valueOf(myInt).split("")).mapToInt(Integer::parseInt).toArray();
Generic solution:
import java.util.stream.Stream;
public static int[] convertValueToIntegerArray(int value) {
return Stream.of(String.valueOf(value).split(""))
.mapToInt(Integer::parseInt)
.toArray();
}
Example:
import java.util.Arrays;
public void testIntegerArray() {
int myInt = 4821;
int[] myIntArr = convertValueToIntegerArray(myInt);
System.out.println("Converted: " + Arrays.toString(myIntArr));
}
答案2
得分: 1
public static void main(String[] args) {
int myInt = 4821;
int size = 0;
int temp = myInt;
while(temp > 0) {
temp = temp/10;
size++;
}
int[] numbers = new int[size];
int index = 0;
while(myInt > 0) {
numbers[index] = myInt % 10;
myInt = myInt / 10;
index++;
}
for (int i : numbers) {
System.out.println("Number : " + i);
}
}
output
Number : 1
Number : 2
Number : 8
Number : 4
英文:
public static void main(String[] args) {
int myInt = 4821;
int size = 0;
int temp = myInt;
while(temp > 0) {
temp = temp/10;
size++;
}
int[] numbers = new int[size];
int index = 0;
while(myInt > 0) {
numbers[index] = myInt % 10;
myInt = myInt / 10;
index++;
}
for (int i : numbers) {
System.out.println("Number : "+i);
}
}
output
Number : 1
Number : 2
Number : 8
Number : 4
答案3
得分: 0
有很多种方法可以实现。一种简洁的方法是使用 Stream
API,如下所示:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int myInt = 4821;
int[] arr =
String.valueOf(Math.abs(myInt)) // 将绝对值转换为字符串
.chars() // 从字符串中获取 IntStream
.map(c -> c - 48) // 将每个字符转换为数字值
.toArray(); // 将流转换为数组
// 显示数组
System.out.println(Arrays.toString(arr));
}
}
输出:
[4, 8, 2, 1]
注:
'0'
的 ASCII 值 为48
,'1'
的为49
,以此类推。Math#abs
返回一个int
值的绝对值- 例如:
Math.abs(-5) = 5
,Math.abs(5) = 5
- 例如:
英文:
There can be so many ways to do it. A concise way of doing it's using Stream
API as shown below:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int myInt = 4821;
int[] arr =
String.valueOf(Math.abs(myInt)) // Convert the absolute value to String
.chars() // Get IntStream out of the String
.map(c -> c - 48) // Convert each char into numeric value
.toArray(); // Convert the stream into array
// Display the array
System.out.println(Arrays.toString(arr));
}
}
Output:
[4, 8, 2, 1]
Notes:
- ASCII value of
'0'
is48
, that of'1'
is49
and so on. Math#abs
returns the absolute value of anint
value- e.g.
Math.abs(-5) = 5
andMath.abs(5) = 5
- e.g.
答案4
得分: -2
int myInt = 4821;
String myIntText = myInt + ""; //隐式转换为字符串
Integer[] numberArray = new Integer[myIntText.length()]; //创建用于结果的数组
for (int i = 0; i < myIntText.length(); i++) {
String number = myIntText.substring(i, i + 1); //从左到右提取每个数字
numberArray[i] = Integer.valueOf(number); //将数字保存在数组中
}
for (int i = 0; i < myIntText.length(); i++)
System.out.println(numberArray[i]);
英文:
int myInt = 4821;
String myIntText = myInt+ ""; //Implicit cast to String
Integer[] numberArray = new Integer[myIntText.length()]; //Create Array for result
for(int i=0; i<myIntText.length(); i++) {
String number = myIntText.substring(i, i+1); //Extract every number from left to right
numberArray[i] = Integer.valueOf(number); //save the number in the array
}
for(int i = 0; i<myIntText.length(); i++)
System.out.println(numberArray[i]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论