Java Programming Test Question 3
import java.util.HashSet;
public class JPTQuestion3 {
public static void main(String[] args) {
HashSet shortSet = new HashSet();
for (short i = 0; i < 100; i++) {
shortSet.add(i);
shortSet.remove(i - 1);
}
System.out.println(shortSet.size());
}
}
输出:100。
如果把循环变量改为int型的, 那么
import java.util.HashSet;
public class JPTQuestion3 {
public static void main(String[] args) {
HashSet shortSet = new HashSet();
for (int i = 0; i < 100; i++) {
shortSet.add(i);
shortSet.remove(i-1);
}
System.out.println(shortSet.size());
}
}
输出:1
这是什么坑啊。而且,这HashSet竟然不指定泛型就在用了-_-
为什么范围比int小的的就不会被移除,而大于等于int范围的就会被移除?泛型指定与否都与结果无关。
原因:.........................................i-1是int型的,HashSet里面存的是Short类型的,所以,没有一个数字被移除。
官方解释:自动装箱的作用
Java Programming Test Question 3 Answer and Explanation
The size of the shortSet will be 100. Java Autoboxing feature has been introduced in JDK 5, so while adding the short to HashSet<Short> it will automatically convert it to Short object. Now i-1 will be converted to int while evaluation and after that it will autoboxed to Integer object but there are no Integer object in the HashSet, so it will not remove anything from the HashSet and finally its size will be 100.
Java Programming Test Question 3的更多相关文章
- Java Programming Test Question 2
public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev&qu ...
- Java Programming Test Question 4
What will be the boolean flag value to reach the finally block? public class JPTQuestion4 { public s ...
- Java Programming Test Question 1
public class JPTQuestion1 { public static void main(String[] args) { String s1 = "abc"; St ...
- Java programming language compiler
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html\ javac - Java programming l ...
- Java Programming Language Enhancements
引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...
- 文本信息“welcome to java programming!”
import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...
- C Programming vs. Java Programming
Thing C Java type of language function oriented object oriented basic programming unit function clas ...
- Difference Between Arraylist And Vector : Core Java Interview Collection Question
Difference between Vector and Arraylist is the most common Core Java Interview question you will co ...
- Java Programming Guidelines
This appendix contains suggestions to help guide you in performing low-level program design and in w ...
随机推荐
- 转:如何用EXCEL表运用FV函数
转:http://zhidao.baidu.com/link?url=lKFCYBW-zMC-pp8GkFXZnmwQf3YL_csYLGo-0v2OAASSZwjw40QRgEO0V8s2Y3zCJ ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- springMVC配置步骤
所用的工具为eclipse for javaEE,tomcat 8.0 1.导入jar包 把以上的jar包全部复制到项目的WebContent/WEB-INF/lib目录中 2.在webContent ...
- 遍历map集合
for(var key in map){ if(data.hasOwnProperty(key)){ var value = map[key]; } }
- windows下安装使用Composer记录
一直以来都是直接下载类库文件放到项目中使用 Composer是一个PHP依赖管理工具,有了它,就可以对依赖的包进行统一管理,而且在项目中可以实现自动加载 安装和使用composer 下载并且运行 Co ...
- tmux/screen里面如何用鼠标滚轮来卷动窗口内容
tmux里面用鼠标滚轮来卷动窗口内容 在 tmux里面,因为每个窗口(tmux window)的历史内容已经被tmux接管了,所以原来console/terminal提供的Shift+PgUp/PgD ...
- POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)
传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...
- Alpha版本十天冲刺——Day 3
站立式会议 会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 Android端登录验证接口 子线程无法返回从服务器获取的信息. 注册接口.忘记密码接口 开发经验不足,有一个问题不知道怎么解决 ...
- Navicat 的使用(一)
1.创建连接 主机名 : 可以不写名称随意 主机名/IP地址:localhost或者127.0.0.1 都是本机的意思 端口:默认3306 尽量不要改怕与其余端口重复,如有重名端口系统会报错 用户 ...
- WinForm------关于子窗体刷新父窗体问题
链接: http://wenwen.sogou.com/z/q242758397.htm