-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhangwei
committed
Apr 2, 2019
1 parent
5c16d0d
commit 083eef9
Showing
1 changed file
with
18 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 一道面试题 | ||
|
||
> 分享一道面试题,结果有点出乎意料 | ||
代码如下:输出结果是什么? | ||
|
||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
Set<Short> stack = new HashSet<>(); | ||
for (Short i = 0; i < 100; i++) { | ||
stack.add(i); | ||
stack.remove(i - 1); | ||
} | ||
System.out.println(stack.size()); | ||
} | ||
|
||
- 答案是100,i-1这里用到了隐式类型转换i-1会转成int型的减法,所以在remove时,根本没有那个对象,所以无论怎么调stack.remove(i-1)都不会有元素移出,所以答案是100. |