When int and Integer in Java are used for list<integer>, and count the number of program runs and function calls by print variables

Source: Internet
Author: User
Tags netbeans

Summarize the problems that have been encountered in recent work.

1. When int and Integer in Java are used for list<integer>

The relationship between the two is clear, int is the basic data type, the value is stored, and the Integer is the reference data type, which stores the address that points to the value. Integer is the encapsulation of int at the class level. Java then provides an automatic package unpacking mechanism so that the two can be converted. The main question here is to test their doubts about the List.

/* To change this template, choose Tools | Templates * and open the template in the editor. */package readdxflunwenyh;import java.util.linkedlist;import java.util.list;/** * * @author User */public class Test_  Integer {/** * @param args the command line arguments */public static void main (string[] args) {//        TODO Code application logic here list<integer> lst=new LinkedList ();        Lst.add (New Integer (1));        Lst.add (3);        Lst.add (5);        Lst.add (7);        Lst.add (9);          Test1 for (int i=0;i<10;++i) {if (Lst.contains (i) ==false) lst.add (i);          Else System.out.println ("true:" +i);//The result shows that the inclusion relationship is judged directly with the I value of int, which can be correctly judged} for (int i=0;i<lst.size (); ++i) {//output        System.out.print (Lst.get (i) + "");        } System.out.println (); Test2 Lst.remove (1);//results show that the element for which the index 1th number bit is deleted for (int i=0;i<lst.size (); ++i) {//Output System.out.print (LST        . get (i) + ""); } System.out. println (); Test3 Lst.remove (New Integer (1));//results show that the element with value 1 is deleted for (int i=0;i<lst.size (); ++i) {//Output System.ou        T.print (Lst.get (i) + "");    } System.out.println (); }}

Results:

It is found that when list<integer> LST is judged to contain the relationship contains, it can be correctly judged by the variable of type int, and when removing remove, the element of the corresponding index is deleted, instead of the element of that value. The reason, guess should be the contains method is not overloaded, it has only one parameter, is the Object type, then use the int variable to do the argument, it is automatically converted to an Integer type, and the Remove method is overloaded, not only the int type parameter, but also the object shape In this case, when the argument is made with an int variable, the match is directly the element that deletes the corresponding index, the int parameter, and does not convert to an Integer variable. It's also obvious that you want to come here. There is the confusion of remove, perhaps mainly because you can directly use the int variable when add, (and often with the int variable), in the idea of equivalence, you will think that when removing the int variable will be able to delete the value of its elements.

However, it is also doubtful that the,list<integer> is a reference, a reference to the previous 1 or 2 in the program, and a new 1 or 2 reference to be deleted later. Even if 1 is a constant, but 2 is the first time to join by the INT variable, and then delete 2 is a temporary new out of the Integer variable, so that two references are the same?

2. Netbeans Program box and program Count

Before a program ran for a long time, and then there is no output in the process, looking at Netbeans is not moving, thinking that the program where the problem stuck dead. There is a progress bar in the lower right corner of Netbeans, which runs 4 times in a row when the program has output, then turns blue and stops running. (In this case, do not think the program is wrong ~ ~ ~)

A small trick is that if the function is a recursive call, you can add a static variable to record the number of times the function call, and each time it output, not only can see the function is called the number of times, you can also know that the program is running ~ ~ means by printing variables to detect where the program runs, especially in the time of recursion ~

When int and Integer in Java are used for list<integer>, and count the number of program runs and function calls by print variables

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.