Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…java-top-qa into YuxiangQue-master

# Conflicts:
#	contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
  • Loading branch information
giantray committed May 25, 2016
1 parent 7ab16f3 commit f0bc018
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 9 deletions.
123 changes: 114 additions & 9 deletions contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,123 @@
# 如何用0向左补齐一个整数?
## 如何在整数左填充0

## 问题
### 问题
如何在整数左填充0
举例 1 = "0001"

在Java中如何把一个整数转化为字符串并且用0向左补齐呢?

我基本上是希望把一个不大于9999的整数用0补齐 (比如说1=“0001”)。

## 解答
### 答案一,String.format

String.format("%05d", yournumber);

可以将一个五位数用0补齐。
用0填充,总长度为5
https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

### 答案二,ApacheCommonsLanguage
如果需要在Java 1.5前使用,可以利用 Apache Commons Language 方法

org.apache.commons.lang.StringUtils.leftPad(String str, int size, '0')

### 答案三,DecimalFormat
import java.text.DecimalFormat;
class TestingAndQualityAssuranceDepartment
{
public static void main(String [] args)
{
int x=1;
DecimalFormat df = new DecimalFormat("00");
System.out.println(df.format(x));
}
}

### 答案四,自己实现
如果效率很重要的话,相比于 String.format 函数的可以自己实现

/**
* @param in The integer value
* @param fill The number of digits to fill
* @return The given value left padded with the given number of digits
*/
public static String lPadZero(int in, int fill){

boolean negative = false;
int value, len = 0;

if(in >= 0){
value = in;
} else {
negative = true;
value = - in;
in = - in;
len ++;
}

if(value == 0){
len = 1;
} else{
for(; value != 0; len ++){
value /= 10;
}
}

StringBuilder sb = new StringBuilder();

if(negative){
sb.append('-');
}

for(int i = fill; i > len; i--){
sb.append('0');
}

sb.append(in);

return sb.toString();
}

效率对比

public static void main(String[] args) {
Random rdm;
long start;

// Using own function
rdm = new Random(0);
start = System.nanoTime();

for(int i = 10000000; i != 0; i--){
lPadZero(rdm.nextInt(20000) - 10000, 4);
}
System.out.println("Own function: " + ((System.nanoTime() - start) / 1000000) + "ms");

// Using String.format
rdm = new Random(0);
start = System.nanoTime();

for(int i = 10000000; i != 0; i--){
String.format("%04d", rdm.nextInt(20000) - 10000);
}
System.out.println("String.format: " + ((System.nanoTime() - start) / 1000000) + "ms");
}

结果
自己的实现:1697ms
String.format:38134ms

### 答案,Google Guava
Maven:

<dependency>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
<version>14.0.1</version>
</dependency>
样例:

Strings.padStart("7", 3, '0') returns "007"
Strings.padStart("2020", 3, '0') returns "2020"
注意:
Guava 是非常有用的库,它提供了很多有用的功能,包括了Collections, Caches, Functional idioms, Concurrency, Strings, Primitives, Ranges, IO, Hashing, EventBus等

https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

Stackoverflow链接:http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left
stackoverflow原址:
http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left
47 changes: 47 additions & 0 deletions contents/is-null-check-needed-before-calling-instanceof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## 在调用 instanceof 前需要进行null检查吗


### 问题:

null instanceof SomeClass 会返回 null 还是抛出 NullPointerException 异常

### 答案一
在调用 instanceof 前不要进行null检查
null instanceof SomeClass 会返回 null
在 Java Language Specification 中 http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2

```
在运行时,如果该instanceof运算符的关系表达式(RelationExpression)不为 null,且这个引用可以被成功转型( §15.16),不抛出ClassCastException,则结果为true;
否则结果为false。
```

### 答案二
public class IsInstanceOfTest {

public static void main(final String[] args) {

String s;

s = "";

System.out.println((s instanceof String));
System.out.println(String.class.isInstance(s));

s = null;

System.out.println((s instanceof String));
System.out.println(String.class.isInstance(s));
}
}
打印出

true
true
false
false

### 原文链接
http://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof



0 comments on commit f0bc018

Please sign in to comment.