如何将一个整数转换为Java中的整数数组?

huangapple go评论72阅读模式
英文:

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]

注:

  1. '0'ASCII 值48'1' 的为 49,以此类推。
  2. Math#abs 返回一个 int 值的绝对值
    • 例如:Math.abs(-5) = 5Math.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:

  1. ASCII value of '0' is 48, that of '1' is 49 and so on.
  2. Math#abs returns the absolute value of an int value
    • e.g. Math.abs(-5) = 5 and Math.abs(5) = 5

答案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+ &quot;&quot;; //Implicit cast to String
	
Integer[] numberArray = new Integer[myIntText.length()]; //Create Array for result
for(int i=0; i&lt;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&lt;myIntText.length(); i++) 
	System.out.println(numberArray[i]);

huangapple
  • 本文由 发表于 2020年10月13日 04:31:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64324978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定