如何附加到整数

时间:2017-12-14 06:56:45

标签: java append concatenation

在我一直在努力的项目中,我遇到了一个问题,我需要在Integer附加一个数字。问题是您无法通过IntegerInteger.append(someArrayOfNumbers)这样的简单解决方案附加到someDigit.append(aNumber).,并且其中并没有任何有关它的信息。所以我想知道一些算法和" 这样的"。

3 个答案:

答案 0 :(得分:2)

一种解决方法是将int乘以10并添加数字,或者如果int为负数,则减去数字:

pow(10, log10(yourDigits) + 1)

如果要追加任意数量的数字,请将整数乘以Integer.parseInt(Integer.toString(yourInt) + Integer.toString(yourDigit))

另一个解决方案是将int转换为字符串并在字符串末尾添加数字,并将其转换回int:

<main class="set-callback-main-wrap">
     <div class="row">
        <div class="col-xs-12">
          <div class="callback-table-main-wrap">
            <div class="call-back-time-header clearfix">
              <div class="call-back-time-value blank-slot">
                <span></span>
              </div>
              <div class="call-back-time-value first-slot">
                <span>12am</span>
              </div>



            </div>
            <div class="clearfix table-date-value">
              <div class="call-back-date">
                <span>Dec 1</span>
              </div>
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner cyan"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
              <!-- <div class="call-set-slot"> -->
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
                <div class="call-set-slot-inner"></div>
              <!-- </div> -->
            </div>


          </div>

        </div>
      </div>
    </main>

答案 1 :(得分:1)

这是我的 基础 解决方案(好吧部分我的,我从这个答案得到了一些信息,但我不得不修补, here),不使用String。我说 base 因为如果其中一个数组成员超过一位数,这个算法就无法正常工作。

public int append(int[] arrayOfIntegers) {

    int total;
    int length = arrayOfIntegers.length; // for quick reference

    total = (int) (arrayOfIntegers[0] * Math.pow(10, length - 1)); // first item times 10 to the power of length minus 1
    for (int i = 1; i < length; i++) {
        total += arrayOfIntegers[i] * Math.pow(10, length - i - 1);
    }
    return total;
}

现在附加一个整数:

int[] array = { 1, 2, 3, 4, 5, 6, 7 };
System.out.println("The array appended: " + append(array));

输出:

The array appended: 1234567

答案 2 :(得分:0)

如果您将仅一位附加到右侧或LSB,则可以通过乘以10并添加新数字来附加...

public int append(int originalValue, int numberToAppend ) {

     return originalValue*10+ numberToAppend;
}

您可以使用首先使用字符串连接来执行更多数字

public int append(Integer originalValue, Integer numberToAppend ) {
     String temp =  originalValue.toString()+ numberToAppend.toString();
     return Integer.parseInt(temp);
}