删除int数组中的前导零

时间:2018-10-09 12:25:42

标签: java arrays int

尝试通过删除Int数组中的前导零来合并数字。

int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };
for (int i = 0; i < arr.length; i++) {
    String.format("%09d", array[i]);
}

for (int i = 0; i < arr.length; i++) {
    System.out.print(array[i]);
}

所需的输出:

405

7 个答案:

答案 0 :(得分:6)

由于数组的大小固定,因此无法从数组中删除任何元素。因此,我假设您想要一个没有前导零的新数组。

使用Java 9+(Java 8中没有dropWhile),您可以通过以下方式做到这一点:

int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };
int[] withoutLeadingZeros = Arrays.stream(array).dropWhile(i -> i == 0).toArray();
System.out.println(Arrays.toString(withoutLeadingZeros)); // [4, 0, 5]

编辑
如果结果打算是String

String withoutLeadingZeros = Arrays.stream(array).dropWhile(i -> i == 0).collect(StringBuilder::new, StringBuilder::append, (l, r) -> l.append(r)).toString();
System.out.println(withoutLeadingZeros); // 405

编辑
如果结果打算是int

int number = Arrays.stream(array).reduce((l, r) -> l * 10 + r).getAsInt();
System.out.println(number); // 405

答案 1 :(得分:1)

您可以通过插入布尔值来实现。

  boolean leadingZeroFinished=false;
  for (int i = 0; i < arr.length; i++) {
       if(array[i]!=0) {
           leadingZeroFinished=true;
       }
       if(leadingZeroFinished) {
          System.out.println(array[i])
       }
  }

如果您还想删除该元素,则最好使用一个列表

答案 2 :(得分:1)

<Polygon><outerBoundaryIs><LinearRing><coordinates>45.10104177964908,40.984822547610463 45.10090968692046,40.984794073791363 45.100664453229989,40.984794288187921 45.100381490544628,40.984794534920972 45.100041958345791,40.984809124372475 45.099702467867473,40.984852304107442 45.099325184737005,40.984852629781955 45.099116474514659,40.984822690291566 45.099104063174615,40.984665841911927 45.099013335582548,40.984479864140006 45.098741570286684,40.984215717358019 45.098392367529009,40.98400058063185 45.097939872533026,40.98385409162384 45.096957374212259,40.983561153661405 45.096634140628495,40.983434126587397 45.096466014754938,40.983326554685931 45.096129719519915,40.983101617293585 45.09585804107784,40.982886418084632 45.095237420695774,40.982622542596019 45.09488836500072,40.982505325718741 45.094397369446725,40.982525314570182 45.09355744561546,40.982506414387522 45.093143709295106,40.982340277569314 45.092794562715326,40.982154512329011 45.092393727470622,40.981958985567609 45.09204466667903,40.981831966281753 45.09159219883869,40.981675649478788 45.091100898518086,40.981470407064265 45.090583501550341,40.981197865177556 45.090982038868603,40.981085650651345 45.091340398925666,40.981056776842337 45.091660971254555,40.980985048113268 45.091736522760058,40.981056462314172 45.091774507224642,40.981242266326056 45.091963436382507,40.981456540445748 45.092076734586151,40.981542220419612 45.092284329959149,40.981613528717105 45.092472982299867,40.981627671866022 45.092718324433875,40.981713244314605 45.093057983058344,40.981798741046909 45.093341013615415,40.981855692205784 45.093661729191709,40.98188402162458 45.093982406834485,40.981883760648678 45.094303083284338,40.981883498781777 45.094567107465508,40.98184039775763 45.094854207396452,40.981772148586813 45.094868818813318,40.981768674687309 45.095170629872008,40.981768425930277 45.095397010544467,40.981782534042672 45.095440931885499,40.981826812052375 45.095529363043667,40.981996849097435 45.095774917277701,40.982225364318623 45.096077023213212,40.982425242361359 45.096397950846125,40.982596513438089 45.096718881328677,40.98276778451433 45.096926485530894,40.982839084427589 45.097171879858919,40.982953237762125 45.097473760624808,40.982995866756063 45.097870045107712,40.98309559661061 45.098134242486466,40.983166846285897 45.098455116441038,40.983295226918223 45.098926965007266,40.98346636283835 45.099210114826448,40.983594773727148 45.099417766043985,40.983694659526414 45.099606513038339,40.983765971761535 45.099795171865196,40.983780102915382 45.099983875140275,40.983822824145648 45.100229171354265,40.983865496778819 45.100399140982461,40.983994003081726 45.100493611840839,40.984093984546156 45.100625876821965,40.984236819482184 45.100814712547454,40.984365308694699 45.100965646958933,40.98437947091454 45.101116625104254,40.984422223319044 45.101211010240384,40.984465026124859 45.1012112506451,40.984622270521207 45.10104177964908,40.984822547610463</coordinates></LinearRing></outerBoundaryIs></Polygon>

答案 3 :(得分:0)

如果要打印,请尝试以下操作:

int[] arr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };
boolean isNotLeadingZero=false;
for (int i = 0; i < arr.length; i++) {
    if((arr[i] != 0)  || isNotLeadingZero){
        System.out.print(arr[i]);
        isNotLeadingZero= true;
    }
}

输出

405

答案 4 :(得分:0)

您可以使用以下代码:

boolean foundNonZero = false;
for (int i = 0; i < arr.length; i++) {
    foundNonZero |= (arr[i] != 0);
    if(foundNonZero) {
       System.out.print(array[i]);
    }
}

答案 5 :(得分:0)

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] arr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };

        int i,j;

        for (i = 0; i < arr.length; i++) {
            if(arr[i]!=0)
                break;
        }

        j=0;

        for(;i<arr.length;i++)
        {
            arr[j]=arr[i];
            j++;
        }



        for (i = 0; i < j; i++) {
            System.out.print(arr[i]);
        }
    }
}

我已按照您要求删除前导零的方式移动了元素

答案 6 :(得分:0)

将数组转换为数字然后打印:

    int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };
    int exp = 1;
    long total =0;
    for (int i = array.length -1; i >=0; i-- ) {
        total += array[i]*exp;
        exp *=10;
    }


    System.out.println(total);

然后,您甚至可以将结果用于进一步的计算。