Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0bc018

Browse files
committed
Merge branch 'master' of https://github.com/YuxiangQue/stackoverflow-java-top-qa into YuxiangQue-master
# Conflicts: # contents/how-can-i-pad-an-integers-with-zeros-on-the-left.md
1 parent 7ab16f3 commit f0bc018

File tree

2 files changed

+161
-9
lines changed

2 files changed

+161
-9
lines changed
Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,123 @@
1-
#如何用0向左补齐一个整数?
1+
## 如何在整数左填充0
22

3-
## 问题
3+
### 问题
4+
如何在整数左填充0
5+
举例 1 = "0001"
46

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

7-
我基本上是希望把一个不大于9999的整数用0补齐 (比如说1="0001")。
8-
9-
## 解答
8+
### 答案一,String.format
109

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

13-
可以将一个五位数用0补齐。
12+
用0填充,总长度为5
13+
https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
14+
15+
### 答案二,ApacheCommonsLanguage
16+
如果需要在Java 1.5前使用,可以利用 Apache Commons Language 方法
1417

18+
org.apache.commons.lang.StringUtils.leftPad(String str, int size, '0')
19+
20+
### 答案三,DecimalFormat
21+
import java.text.DecimalFormat;
22+
class TestingAndQualityAssuranceDepartment
23+
{
24+
public static void main(String [] args)
25+
{
26+
int x=1;
27+
DecimalFormat df = new DecimalFormat("00");
28+
System.out.println(df.format(x));
29+
}
30+
}
31+
32+
### 答案四,自己实现
33+
如果效率很重要的话,相比于 String.format 函数的可以自己实现
34+
35+
/**
36+
* @param in The integer value
37+
* @param fill The number of digits to fill
38+
* @return The given value left padded with the given number of digits
39+
*/
40+
public static String lPadZero(int in, int fill){
41+
42+
boolean negative = false;
43+
int value, len = 0;
44+
45+
if(in >= 0){
46+
value = in;
47+
} else {
48+
negative = true;
49+
value = - in;
50+
in = - in;
51+
len ++;
52+
}
53+
54+
if(value == 0){
55+
len = 1;
56+
} else{
57+
for(; value != 0; len ++){
58+
value /= 10;
59+
}
60+
}
61+
62+
StringBuilder sb = new StringBuilder();
63+
64+
if(negative){
65+
sb.append('-');
66+
}
67+
68+
for(int i = fill; i > len; i--){
69+
sb.append('0');
70+
}
71+
72+
sb.append(in);
73+
74+
return sb.toString();
75+
}
76+
77+
效率对比
78+
79+
public static void main(String[] args) {
80+
Random rdm;
81+
long start;
82+
83+
// Using own function
84+
rdm = new Random(0);
85+
start = System.nanoTime();
86+
87+
for(int i = 10000000; i != 0; i--){
88+
lPadZero(rdm.nextInt(20000) - 10000, 4);
89+
}
90+
System.out.println("Own function: " + ((System.nanoTime() - start) / 1000000) + "ms");
91+
92+
// Using String.format
93+
rdm = new Random(0);
94+
start = System.nanoTime();
95+
96+
for(int i = 10000000; i != 0; i--){
97+
String.format("%04d", rdm.nextInt(20000) - 10000);
98+
}
99+
System.out.println("String.format: " + ((System.nanoTime() - start) / 1000000) + "ms");
100+
}
101+
102+
结果
103+
自己的实现:1697ms
104+
String.format:38134ms
105+
106+
### 答案,Google Guava
107+
Maven:
108+
109+
<dependency>
110+
<artifactId>guava</artifactId>
111+
<groupId>com.google.guava</groupId>
112+
<version>14.0.1</version>
113+
</dependency>
114+
样例:
115+
116+
Strings.padStart("7", 3, '0') returns "007"
117+
Strings.padStart("2020", 3, '0') returns "2020"
118+
注意:
119+
Guava 是非常有用的库,它提供了很多有用的功能,包括了Collections, Caches, Functional idioms, Concurrency, Strings, Primitives, Ranges, IO, Hashing, EventBus等
15120

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

18-
Stackoverflow链接:http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left
122+
stackoverflow原址:
123+
http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## 在调用 instanceof 前需要进行null检查吗
2+
3+
4+
### 问题:
5+
6+
null instanceof SomeClass 会返回 null 还是抛出 NullPointerException 异常
7+
8+
### 答案一
9+
在调用 instanceof 前不要进行null检查
10+
null instanceof SomeClass 会返回 null
11+
在 Java Language Specification 中 http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2
12+
13+
```
14+
在运行时,如果该instanceof运算符的关系表达式(RelationExpression)不为 null,且这个引用可以被成功转型( §15.16),不抛出ClassCastException,则结果为true;
15+
否则结果为false。
16+
```
17+
18+
### 答案二
19+
public class IsInstanceOfTest {
20+
21+
public static void main(final String[] args) {
22+
23+
String s;
24+
25+
s = "";
26+
27+
System.out.println((s instanceof String));
28+
System.out.println(String.class.isInstance(s));
29+
30+
s = null;
31+
32+
System.out.println((s instanceof String));
33+
System.out.println(String.class.isInstance(s));
34+
}
35+
}
36+
打印出
37+
38+
true
39+
true
40+
false
41+
false
42+
43+
### 原文链接
44+
http://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof
45+
46+
47+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /