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 ...
随机推荐
- MWeb
专业的 Markdown 写作支持 极简 UI.Dark Mode.漂亮的 Markdown 语法高亮.列表缩进优化,提供 5 种主题选择. 除了支持基本的 Markdown 语法外,还支持大量 Ma ...
- [模板]tarjan求强连通分量
大约是今年4月学的算法了,后来5月的时候做题还写了一个退化的tarjanQAQ. 时间复杂度:O(n+m) 用途:有向图缩环 #include<set> #include<cmath ...
- js-FCC算法Friendly Date Ranges
让日期区间更友好! 把常见的日期格式如:YYYY-MM-DD 转换成一种更易读的格式. 易读格式应该是用月份名称代替月份数字,用序数词代替数字来表示天 (1st 代替 1). 记住不要显示那些可以被推 ...
- 【caffe】train_lenet.sh在windows下的解决方案
@tags: caffe python 在windows下配置caffe后,跑mnist手写数字识别的例子.发现train_lenet.sh不能运行. 那就写个python脚本替代吧. step1 定 ...
- 【poj2761】 Feed the dogs
http://poj.org/problem?id=2761 (题目链接) 题意 求区间第K大. Solution 和poj2104一模一样. 主席树代码 // poj2761 #include< ...
- bzoj3514Codechef MARCH14 GERALD07加强版
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- C#点击按钮关闭当前窗体 打开另一个窗体。
网上有很多是隐藏当前窗体,但是这样占用资源,效果不好,因此改进方法如下: private void button1_Click(object sender,EventArgs e) { this.hi ...
- 企业开发中选择logback而不是log4j的理由
不知道看到这篇文章的Java工程师有没有考虑过这个问题:为什么在企业开发中会选择logback来记录日志,而不是log4j呢? 如果你以前没有考虑过这个问题,那么现在如果让你考虑一下,你可能觉的会是因 ...
- MooseFs-分布式文件系统系列(四)之简单聊聊MFS的日常维护
回顾 文件或目录的额外属性(noower,noattracache和noentrycache),可以通过MFS提供的命令(mfsgeteattr,mfsseteattr,mfsdeleattr等)检查 ...
- JavaWeb---总结(十七)JSP中的九个内置对象
一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...