Java Programming Test Question 3

Source: Internet
Author: User

Import Java.util.HashSet;  Public class JPTQuestion3 {    publicstaticvoid  main (string[] args) {          New  HashSet ();          for ( short i = 0; i < i++) {            shortset.add (i);             -1);        }        System.out.println (Shortset.size ());}    }

Output: 100.

If you change the loop variable to int type, then

Import Java.util.HashSet;  Public class JPTQuestion3 {    publicstaticvoid  main (string[] args) {          New  HashSet ();          for (int i = 0; i < i++) {            shortset.add (i);            Shortset.remove (i-1);        }        System.out.println (Shortset.size ());}    }

Output: 1

What a pit it is. Moreover, this hashset actually does not specify the generic type to use the-_-

Why is the range smaller than int not removed, and greater than or equal to the int range will be removed? Generics are specified regardless of the result.

Java Programming Test Question 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.