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的更多相关文章

  1. Java Programming Test Question 2

    public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev&qu ...

  2. Java Programming Test Question 4

    What will be the boolean flag value to reach the finally block? public class JPTQuestion4 { public s ...

  3. Java Programming Test Question 1

    public class JPTQuestion1 { public static void main(String[] args) { String s1 = "abc"; St ...

  4. Java programming language compiler

    https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html\ javac - Java programming l ...

  5. Java Programming Language Enhancements

    引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...

  6. 文本信息“welcome to java programming!”

    import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...

  7. C Programming vs. Java Programming

    Thing C Java type of language function oriented object oriented basic programming unit function clas ...

  8. 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 ...

  9. Java Programming Guidelines

    This appendix contains suggestions to help guide you in performing low-level program design and in w ...

随机推荐

  1. Matlab读入含有特殊分隔符的文件(textread)

    笔者在此基础上进行运行,修改得到以下内容,希望大家给与补充: textread 基本语法是: [A,B,C,…] = textread(filename,format) [A,B,C,…] = tex ...

  2. 【uoj58】 WC2013—糖果公园

    http://uoj.ac/problem/58 (题目链接) 题意 给定一棵树,每个点有一个颜色,提供两种操作: 1.询问两点间路径上的${\sum{v[a[i]]*w[k]}}$,其中${a[i] ...

  3. 针对CMS中的tag标签理解

    针对CMS的tag标签有以下解释: 什么tag标签? TAG标签是一种由自定义的一种标签,要比分类更加的准确,可以概括文章主要内容的关键词. 运用TAG标签,可以使网站的文章更容易被搜索引擎检索到.百 ...

  4. Bzoj3004 吊灯

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 72  Solved: 46 Description        Alice家里有一盏很大的吊灯.所 ...

  5. 添加一个功能Action

    1,只用一个handler类,所有都事件的处理器都在一个handler类 handler要创建以Action为名称的方法 event要单独分开,继承KDEvent package com.kingde ...

  6. rsync服务器安装配置

    #rsync指定端口号(10002)1 rsync -e 'ssh -p 10002' rsync (server -- client) #2014-3-3 -----------server---- ...

  7. Linux之:Ubuntu速学笔记(2)

    撰写日期:2016-7-3 18:20:39 基本内容包括:Flash player安装.编译安装PHP.写个简单的PHP程序:Java程序(Java需要使用“javac”命令编译一下才能执行) 一. ...

  8. Exception:A generic error occurred in GDI+

    分析: 一般出现这种问题都是GDI和原数据(比如Bitmap)是同一个实体,只不过是两个引用.换句话说就是这个路径的图片被GDI占用啦. 还有一种情况是路径有问题. 场景一: WPF的Image控件的 ...

  9. Yocto开发笔记之《驱动调试-华为3G模块》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== 参考:ht ...

  10. UI学习之常用方法(续)

    UIView 1.    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(100,100, 100, 30)]; [vv.layer setB ...