代码之家  ›  专栏  ›  技术社区  ›  Mohammad Sepahvand

将数组中的整数拆分为单个数字

  •  1
  • Mohammad Sepahvand  · 技术社区  · 14 年前

    我有一个数组:

    int test[]={10212,10202,11000,11000,11010};
    

    我想将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};
    

    我该怎么做呢?我用Java做这个。

    谢谢您。

    7 回复  |  直到 14 年前
        1
  •  10
  •   CheesePls    14 年前
    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;
        }
    }
    

    通过将条目的最低顺序数字放入arraylist的“front”中,从而放在先前的低顺序数字/条目之前,这将完全符合您的要求。

    如果需要它在数组中,arraylist有一个toArray方法。

        2
  •  5
  •   Jack    14 年前

    你可以按照马克的建议去,或者把它们转换成 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'));
    }
    

    因为更节省内存的方法仍然涉及字符串,所以将所有数字保持为一个字符串并计算 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');
      }
    }
    

    介意 奶酪蛋糕 解决方案是 正确的 一个是因为它按预期使用数学。我的只是个傻瓜(只是想给这个问题另一种解决方法)。

        3
  •  1
  •   Roman    14 年前

    几乎只有一行(如果我们假设有一个开箱即用的函数,用于将字符串数组转换为整数数组):

    int test[]={10212,10202,11000,11000,11010};
    int result[] = strArray2IntArray (
                      Arrays.toString (test)
                     .replaceAll ("\\D", "")
                     .replaceAll ("(\\d)", "$1 ")
                     .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;
    }
    
        4
  •  0
  •   High Performance Mark    14 年前

    取每个整数,除以10,然后将小数与余数分开。将小数乘以10,使其再次成为整数,并将其放入新数组中。重复直到你的数字用完。重复,直到输入数组中的整数用完为止。

        5
  •  0
  •   zmbush    14 年前

    试试这样的。

    {
      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.
        }
      }
    }
    

    我没有测试过这个代码。

    这段代码将忽略前导零(自然地)并回填整数数组。获取数组的正确大小可能是个问题。

        6
  •  0
  •   Timmmm    14 年前

    这个(未测试):

    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;
      }
    }
    
        7
  •  0
  •   Carl Manaster    14 年前

    这是一种用字符串来实现的方法。不是特别出色,但(我希望)容易理解。

    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]);
        }
    
    }