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范围的就会被移除?泛型指定与否都与结果无关。

时间: 2024-10-12 19:20:11

Java Programming Test Question 3的相关文章

Java Programming Test Question 4

What will be the boolean flag value to reach the finally block? public class JPTQuestion4 { public static void main(String[] args) { try { if (flag) { while (true) { } } else { System.exit(1); } } finally { System.out.println("In Finally"); } }

Java Programming Test Question 2

public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev"; int start = 1; char end = 5; System.out.println(start + end); System.out.println(s3.substring(start, end)); } } 注意:这里的end是char类型,可是,输出结果和int型的end没区别.

Get your Advanced Java Programming Degree with these Tutorials and Courses

Getting started as a Java developer these days is quite straightforward. There are countless books on the subject, and of course an abundance of online material to study. 最近,入门成为一名java开发人员是非常简单的.有无相关的书籍,当然还有大量的在线资料可供学习 Of course, our own site offers

Java Programming Tutorial Java Native Interface (JNI)

1.  Introduction At times, it is necessary to use native codes (C/C++) to overcome the memory management and performance constraints in Java. Java supports native codes via the Java Native Interface (JNI). JNI is difficult, as it involves two languag

[JAVA Programming] 关于JList的一点笔记

这次写JAVA课的大作业,首先不得不佩服所给的dictionary.txt文件的厉害之处啊,各种大小写.连字符还有各种词组的不同情况在自己测试的时候都中奖了,我该高兴么... 其实要求不高,大概就是一个词典的查询软件,提供了后台词典,只要完成其中的文件I/O,进行String的处理就可以了. 下面其实主要是一些算法问题,查找的话,既然有序(但是从某些角度说,'-'的值要比a-z小啊,但是在dictionary中的顺序却不是这样啊~~所以我暴力地进行了一次QuickSort...)果断O(logn

C Programming vs. Java Programming

Thing C Java type of language function oriented object oriented basic programming unit function class = ADT portability of source code possible with discipline yes portability of compiled code no, recompile for each architecture yes, bytecode is "wri

WebSocket Java Programming入门-1(annotated)

1.前言 一直没有怎么做过前端的东西,但是最近的项目中,前端人员奇缺,公司又不安排新的人员进入,所以我这个后台开发人员只能拉过来坐前端了,前段的东西感觉一大堆,CSS,js自不必说,HTML生态圈就有很多的技术要去学习,好吧,那就一个一个的学习整理啦,先来说说最近这个项目的前端用到什么技术吧. 1.Restful:DropWizard这个很简单,两天基本上就能拿下 2.Js Framework :AngularJS 这个JS库的确很酷,最近学习的过程中越来越喜欢这个东西了,打算以后再整理一些An

Chapter 2 The Java Programming Environment

1. Installing the Java Development kit (JDK) 2. Choosing a Develpment Environment 3. Using the Command-Line Tools 4. Using an Integrated Development Environment 5. Running a Graphical Application 6. Building and Running Applets In this chapter, we wi

Introduce to Java Programming:NOTE

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=default"></script> 博客中要分别讨论的java专题: Calendar类 File类 Number类 测试文章的方式来看 计算机中字节(byte)是最小的存储单元,1byte=8bits. 内存:随机访问存储器(RAM).内存中每一个字节都有其