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

发布于 2024-08-30 03:15:50 字数 279 浏览 5 评论 0原文

我有一个数组:

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做这个。

谢谢。

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:

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.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

情何以堪。 2024-09-06 03:15:50
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 有一个 toArray 方法。

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.

-柠檬树下少年和吉他 2024-09-06 03:15:50

您可以按照 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'));
}

由于仍然涉及字符串的更内存有效的方法是将所有数字保留为一个字符串并计算 < code>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');
  }
}

请注意,CheesePls 解决方案是正确的解决方案,因为它按照预期使用数学。我的只是为了菜鸟(并且只是为了提供解决问题的另一种方法)..

You can go as suggested by Mark, or convert them to String to get the single digits:

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 int value on the fly:

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 CheesePls solution is the right one 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)..

此刻的回忆 2024-09-06 03:15:50

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

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

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)", "$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;
}
我的影子我的梦 2024-09-06 03:15:50

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

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.

江城子 2024-09-06 03:15:50

尝试这样的事情。

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

我没有测试过这段代码。

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

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.

苦行僧 2024-09-06 03:15:50

这个(未经测试):

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

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;
  }
}
寄意 2024-09-06 03:15:50

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

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

}

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

}
九命猫 2024-09-06 03:15:50
    package Map;
    import java.util.ArrayList;
    public class Practice7 { 
        public void testing() {
            int test[] = {10212,10202,11000,11000,11010};
            ArrayList<Integer> temp = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<test.length;i++){
                if(test[i]>0){
    
                    sb.append(test[i]);
                    String s= sb.toString();
                    //System.out.println(s);
                    for(int m=0;m<s.length();m++){
                        //System.out.println(s.charAt(m));
                        temp.add((int) s.charAt(m)-'0');
                    }
    
                    for(int b: temp){
                        System.out.println(b);
                    }
    
                }
            }
    
    
    
    
    
        }
        public static void main(String[] args) {
            Practice7 practice7= new Practice7();
            practice7.testing();
    
    
        }
    }
    package Map;
    import java.util.ArrayList;
    public class Practice7 { 
        public void testing() {
            int test[] = {10212,10202,11000,11000,11010};
            ArrayList<Integer> temp = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<test.length;i++){
                if(test[i]>0){
    
                    sb.append(test[i]);
                    String s= sb.toString();
                    //System.out.println(s);
                    for(int m=0;m<s.length();m++){
                        //System.out.println(s.charAt(m));
                        temp.add((int) s.charAt(m)-'0');
                    }
    
                    for(int b: temp){
                        System.out.println(b);
                    }
    
                }
            }
    
    
    
    
    
        }
        public static void main(String[] args) {
            Practice7 practice7= new Practice7();
            practice7.testing();
    
    
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文