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 ...
随机推荐
- Leetcode 160. Intersection of two linked lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 【bzoj1014】 JSOI2008—火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 (题目链接) 题意 给出一个字符串,要求维护这些操作:询问后缀x与后缀y的LCQ(最长公共前缀 ...
- bzoj2683简单题
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> ...
- CommonJS/AMD/CMD/UMD概念初探
1.CommonJS是一种规范,NodeJS是这种规范的实现. 1.1.CommonJS 加载模块是同步的,所以只有加载完成才能执行后面的操作. 参考: http://www.commonjs.org ...
- ExceptionLess新玩法 -- 审计日志
审计日志 这算是一个挺酷的功能,把每个请求都记录下来,之前在abp中看到过这个功能,配合可视化的界面,简直是在装逼 看到了exceptionless后,心念一动,我也可以根据它做一个审计日志的功能.这 ...
- 资料推荐--Google Java编码规范
之前已经推荐过Google的Java编码规范英文版了: http://google-styleguide.googlecode.com/svn/trunk/javaguide.html 虽然这篇文章的 ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- Code笔记之:对使用zend加密后的php文件进行解密
对使用zend加密后的php文件进行解密 使用zend加密后的php文件用notpad++打开会出现类似的乱码 下面使用解密工具进行解密 http://pan.baidu.com/s/1i3n4ysX ...
- C++ 动态数组实例
一维动态数组的实例: #include <iostream> using namespace std; int main() { int *arr; int n; cout<< ...
- C#保存图片设置图片质量的方法
主要用到System.Drawing.Imaging命名空间下的ImageCodecInfo.Encoder.EncoderParameter.EncoderParameters四个类 实现代码 代码 ...