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 ...
随机推荐
- BZOJ1010 [HNOI2008]玩具装箱toy
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- Android成长日记-Activity
① Activity是一个应用程序组件,提供用户与程序交互的界面 ② Android四大组件 ---Activity ---Service ---BroadcastReceiver ---Conten ...
- jQuery—选择器
摘抄自<锋利的jQuery> 一.基本选择器 $("#one").css("background","#bbffaa"); 选取 ...
- 无法启动此程序,因为计算机中丢失MSVCP110.dll
安装Visual C++ Redistributable for Visual Studio 2012 有arm.x86.x64有三个版本. 如果应用程序为debug版本而不是release版本,可能 ...
- Linux 平台PostGIS安装
1.前提条件: postgresql 9.6.1 已经通过源码方式安装完成并可成功运行. 2. other OS packets OS: CentOS 6.4 X64 X64: libxml2-dev ...
- NSClassFromString 和 遍历UIView获取她所在的UIViewController的tips
一.NSClassFromString是NSObjCRuntime.h的方法 FOUNDATION_EXPORT Class __nullable NSClassFromString(NSString ...
- JZOJ P1830[9.30]送牛奶
传送门 临近NOIp,写一些简单题. 二分+BFS,注意的是要把数组开小点,有效减少memset的时间. //OJ 1830 //by Cydiater //2016.9.22 #include &l ...
- JavaScript DES 加密tripledes.js:
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- 解决centos7中python-pip模块不存在的问题
centos 7中python-pip模块不存在,是因为像centos这类衍生的发行版,源跟新滞后,或者不存在.即使使用yum去search python-pip也找不到软件包. 为了使用安装滞后或源 ...
- JavaScript中捕获/阻止捕获、冒泡/阻止冒泡
JavaScript中捕获/阻止捕获.冒泡/阻止冒泡 事件流描述的是从页面中接收事件的顺序.提出事件流概念的正是IE和Netscape,但是前者提出的是我们常用的事件冒泡流,而后者提出的是事件捕获流. ...