java 将数组中的整数拆分为单个数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2722742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting integers in arrays to individual digits
提问by Mohammad Sepahvand
I have an array:
我有一个数组:
int test[]={10212,10202,11000,11000,11010};
I want to split the inetger values to individual digits and place them in a new array as individual elements such that my array now is:
我想将 inetger 值拆分为单个数字,并将它们作为单个元素放在一个新数组中,这样我的数组现在是:
int test2[]={1,0,2,1,2,1,0,2,0,2,1,1,0,0,0,1,1,0,0,0,1,1,0,1,0};
How would i go about doing that? I'm doing this in java.
我该怎么做呢?我正在用java做这个。
Thank you.
谢谢你。
回答by CheesePls
int[] test={10212,10202,11000,11000,11010};
ArrayList<Integer> test2 = new ArrayList<Integer>();
for(int i = test.length -1; i >= 0; i--){
int temp = test[i];
while(temp>0){
test2.add(0, temp%10); //place low order digit in array
temp = temp /10; //remove low order digit from temp;
}
}
This will do exactly what you want by placing the lowest order digit of an entry into the "front" of an arraylist, and therefore in front of the previous low order digits/entries.
这将通过将条目的最低位数字放入数组列表的“前面”,从而在之前的低位数字/条目之前完成您想要的操作。
If you need it to be in an Array, ArrayList has a toArray method.
如果你需要它在一个数组中,ArrayList 有一个 toArray 方法。
回答by Hyman
You can go as suggested by Mark, or convert them to Stringto get the single digits:
您可以按照 Mark 的建议进行,或将它们转换String为获得个位数:
int test[]={10212,10202,11000,11000,11010};
ArrayList<Integer> test2 = new ArrayList<Integer>();
for (int i : test)
{
String str = String.valueOf(i);
for (int j = 0; j < str.length(); ++j)
test2.add((int)(str.charAt(j)-'0'));
}
As more memory efficient approach that still involves string would be to keep all the digits as just one string and calculate the intvalue on the fly:
由于仍然涉及字符串的更高效的内存方法是将所有数字保留为一个字符串并动态计算int值:
class Digits
{
String str;
Digits(int[] nums)
{
StringBuilder sb = new StringBuilder();
for (int i : nums)
sb.append(String.valueOf(i));
str = sb.toString();
}
int length()
{
return str.length();
}
int nth(int i)
{
return (int)(str.charAt(i)-'0');
}
}
Mind that CheesePlssolution is the rightone because it uses math as it is intended to be used. Mine is just for noobs (and just to give another approach to the problem)..
请注意CheesePls解决方案是正确的解决方案,因为它按照预期使用数学。我的仅供新手使用(并且只是提供另一种解决问题的方法)。
回答by Roman
Almost one-liner (if we assume that there is an out of the box function for converting array of strings to array of integers):
几乎是单行(如果我们假设有一个开箱即用的函数可以将字符串数组转换为整数数组):
int test[]={10212,10202,11000,11000,11010};
int result[] = strArray2IntArray (
Arrays.toString (test)
.replaceAll ("\D", "")
.replaceAll ("(\d)", " ")
.split (" ")
);
private static int[] strArray2IntArray (String[] array) {
int[] result = new int[array.length];
for (int i = 0; i < result.length; i++) {
result[i] = Integer.parseInt (array[i]);
}
return result;
}
回答by High Performance Mark
You'd take each integer, divide by 10 and separate the fraction from the remainder. multiply the fraction by 10 to make it an integer again and put it into your new array. Repeat until you run out of digits. Repeate until you run out of integers in your input array.
您将取每个整数,除以 10,然后将分数与余数分开。将分数乘以 10 使其再次成为整数并将其放入新数组中。重复直到用完数字。重复直到用完输入数组中的整数。
回答by zmbush
Try something like this.
尝试这样的事情。
{
int test[]={10212,10202,11000,11000,11010};
int test2[] = new int[25];
int i = 24;
int temp;
for(int n = test.size() - 1; n >= 0; n--){
temp = test[n];
while(temp > 0){
test2[i--] = test % 10; // <= Gets remainder of division by 10.
test /= 10; // <= Stores the integer result of the division.
}
}
}
I have not tested this code.
我没有测试过这段代码。
This code would ignore leading zeroes (Naturally) and backfill the array of integers. Getting the proper size for the array could be a problem.
此代码将忽略前导零(自然)并回填整数数组。获取数组的正确大小可能是一个问题。
回答by Timmmm
This (untested):
这个(未经测试):
int test[] = {10212,10202,11000,11000,11010};
ArrayList<Integer> test2 = new ArrayList<Integer>();
for (int i : test)
{
int p = test2.size();
while (i > 0)
{
test2.add(p, i % 10);
i /= 10;
}
}
回答by Carl Manaster
Here's a way to do it with strings. Not particularly performant, but (I hope) easy to understand.
这是一种用字符串来做的方法。不是特别高效,但(我希望)易于理解。
package playground.tests;
import junit.framework.TestCase;
public class SplitIntegerTest extends TestCase {
public void testIntsFromOneInteger() throws Exception {
assertEqualArrays(new int[] { 1, 0, 2, 1, 2 }, intsFrom(10212));
}
public void testIntsFromArray() throws Exception {
int test[] = { 10212, 10202, 11000, 11000, 11010 };
int test2[] = { 1, 0, 2, 1, 2, 1, 0, 2, 0, 2, 1, 1, 0, 0, 0, 1, 1, 0,
0, 0, 1, 1, 0, 1, 0 };
assertEqualArrays(test2, intsFrom(test));
}
private int[] intsFrom(int[] input) {
int[][] list = new int[input.length][];
for (int i = 0; i < input.length; i++)
list[i] = intsFrom(input[i]);
int n = 0;
for (int[] array : list)
n += array.length;
int[] result = new int[n];
int index = 0;
for (int[] array : list)
for (int i : array)
result[index++] = i;
return result;
}
private static int[] intsFrom(Integer n) {
String s = n.toString();
int[] result = new int[s.length()];
for (int i = 0; i < result.length; i++)
result[i] = intFrom(s.charAt(i));
return result;
}
private static int intFrom(Character c) {
return Integer.parseInt(c.toString());
}
private static void assertEqualArrays(int[] a, int[] b) {
assertEquals(a.length, b.length);
for (int i = 0; i < b.length; i++)
assertEquals(a[i], b[i]);
}
}

