简体   繁体   English

如何在数组中添加前缀 0?

[英]How to add prefix 0s in an array?

I am writing a Java program that trims an array of numbers to length 10. If the array is shorter than 10, then the program should prefix the array with 0s until it is length 10.我正在编写一个 Java 程序,将数字数组修剪为长度为 10。如果数组小于 10,则程序应在数组前面加上 0,直到长度为 10。

Example:例子:

 1234567890 :: 1234567890 12345678990:: 1234567899 1234 :: 0000001234
    int[] aa= new int[]{123456789};
    if(aa.length==10)
    {
        for (int i=0;i<=aa.length;i++)
        {
             system.out.println(aa[i]);
        } 
    }
    if(aa.length>0)
    {
        for(int i=0;i<=10;i++)
        {
            system.out.println(aa[i]);
        }
    }
    if(aa.length<10)
    {
        for(int i=0;i<=10;i++)
        {
            system.out.println(aa[i]);
        }
    }

Is there something wrong in my code?我的代码有问题吗?

First of all, arrays of dynamic size makes me think you want to do an ArrayList, not an array.首先,动态大小的数组让我觉得你想做一个ArrayList,而不是一个数组。 Still, if for some reason you are dealing with actual arrays of different length, it will be something like this.尽管如此,如果由于某种原因您正在处理不同长度的实际数组,它将是这样的。

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int[] lessThanTen = new int[] {1,2,3,4};
        int[] newArr = new int[10];

        int numZeros = 10 - lessThanTen.length;

        for (int i = 0; i < 10; i++) {
            if (i < numZeros) {newArr[i] = 0;}
            else {newArr[i] = lessThanTen[i - numZeros];}
        }

        System.out.println(Arrays.toString(newArr));
    }
}

First, an int can't have more than 10 digits so I am going to presume you are using longs .首先, int不能超过 10 位数字,所以我假设您使用的是longs

  1. To determine the number of digits in any integral number, use Math.log10() .要确定任何整数中的位数,请使用Math.log10()
  2. If the number has <= 10 digits, use the printf specifier of %010d which allows for a field of 10 digits, padded on the left with 0's.如果数字具有 <= 10 位数字,请使用%010dprintf说明符,它允许 10 位数字的字段,在左侧用 0 填充。
  3. Otherwise, convert to a string and print the first 10 digits using String.substring();否则,转换为字符串并使用 String.substring() 打印前 10 位数字;
      for (long n : new long[] { 123,12,1, 1234579911111L, 1234567890
      }) {
         if (Math.log10(n) <= 10) {
            System.out.printf("%010d%n", n);
         }
         else {
            System.out.printf("%s%n", Long.toString(n).substring(0, 10));
         }
      }

The above prints the following:以上打印了以下内容:

0000000123
0000000012
0000000001
1234579911
1234567890

If you are reading in the values as strings, then you can just do the following.如果您将值作为字符串读取,那么您只需执行以下操作。

  1. If the string is greater >= 10 characters, take the substring of the first 10.如果字符串大于 >= 10 个字符,则取前 10 个字符的子字符串。
  2. If the string is less < 10, then pad on the left with zeroes, as required.如果字符串小于 < 10,则根据需要在左侧填充零。
      for (String s : new String[] {
            "123", "12", "1", "1234579911111", "1234567890"
      }) {
         int len = s.length();
         String n = len >= 10 ? s.substring(0, 10)
               : "0000000000".substring(len, 10) + s;
         System.out.println(n);
      }

The above prints the following:以上打印了以下内容:

0000000123
0000000012
0000000001
1234579911
1234567890

An integer cannot have 10 digits, use a long instead.整数不能有 10 位数字,请改用long Also, use string to solve the problem rather than arrays.此外,使用字符串而不是数组来解决问题。

public static void main(String[] args) {
    Scanner cmdScanner = new Scanner(System.in);
    System.out.println("Enter a number");
    long num = cmdScanner.nextLong();
    String input = String.valueOf(num);
    if (input.length() > 10) {
        System.out.println(input.substring(0, 10));
    } else {
        final int zerosToAppend = 10 - input.length();
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < zerosToAppend; i++) {
            sb.append("0");
        }
        System.out.println(sb.append(input));
    }
    cmdScanner.close();
}

You can do by simply writing this program:-你可以通过简单地编写这个程序来做到:-

import java.util.Arrays;
public class Test
{
    public static void main(String[] args)
    {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int newarray[] = new int[10];
        //If it is smaller than 10
        if(arr.length<10)
        {
            for(int i=0;i<(10-arr.length);i++)
            {                       
                newarray[i] = 0;
            }
            for(int i=(10-arr.length);i<10;i++)
            {
                newarray[i] = arr[i-(10-arr.length)];
            }
        }
        else
        {
            for(int i=0;i<arr.length;i++)
            {               
                newarray[i] = arr[i];
            }
        }
        System.out.println(Arrays.toString(newarray));
    }
}

Hope this will Help希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM