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 ...
随机推荐
- C++ 序列式容器之vector
什么是容器 容器,顾名思义,是用来容放东西的场所.C++容器容放某种数据结构,以利于对数据的搜寻或排序或其他特殊目的.众所周知,常用的数据结构不外乎:数组array, 链表list, 树tree ...
- java时间和日期类型
在java中,代表时间和日期的类型包括:java.util.Date和java.util.Calendar,此外,在JDBC API中还提供了3个扩展类,java.UtilDate类的子类:java. ...
- bzoj3756: Pty的字符串
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- 文件属性之setuid位
setuid位是可执行文件的一个属性,ls -l /bin/ping 或mount等可以看到权限为-rwsr-xr-x 1 root root 含有s位,所属用户为root表明该文件可以被其他用户以该 ...
- androidstudio 常用快捷键
ctrl+p 查看需要输入的函数列表 ctrl+h 查看继承关系 ctrl +w 选择一个,一行,一段. alter+enter 内容助理 ctrl+q 查看方法注解 相当于 hover ...
- Python 循环判断和数据类型
循环和判断 1.if 形式 if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_ ...
- 三角形问题的解决复杂度O(n^3)和O(nlogn)的比较
问题描述: n条棍子组成一个三角形,使得三角形周少最大. 方法一: 暴力解则算法复杂度为O(n^3) #include<stdio.h> const int MAX_N=105 int m ...
- iOS - Xcode7.3插件实效问题解决方法
以往Xcode升级插件实效.1.关闭xcode 2.终端获取uid 3.文件中info.plist手动添加uid,现在不用了.在网上找的,特地记录一下,尤其是插件太多,不会麻烦了. 详细操作步骤: 关 ...
- MooseFs-分布式文件系统系列(一)之了解并安装它
preface 在上上家公司,曾维护过公司的MFS文件系统,主要用来存储系统日志文件,单纯的把日志当作文件存储,在当时的架构下,MFS就像一个中间站一样,这边程序生成的日志放入MFS,那边日志分析程序 ...
- Diode -- Pay Attention to Parallel Connection
The above circuit is right. The two same resistors are integral. Because every diode is different, t ...