Java Foundation Series -04-Basic type/package type/collections/arrays

Source: Internet
Author: User
Tags shallow copy

Topic One: ValueOf () and Xxxvalue ()

We know that Java has automatic boxing and unpacking functions, this process is the Java directly help us to do, many times is quiet, and is ubiquitous. For example, a variable that assigns a basic value directly to a wrapper type, such as the one we put in the collection, must be an object. And this process is called what method to implement it? Let's take a look at the code and find out:


Take the integer example to see how valueof () and Intvalue () are implemented.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/5B/wKiom1XhgCuzHLECAACAuNo87NQ546.jpg "title=" Sogou _ 2015-08-29_17-49-22.png "alt=" Wkiom1xhgcuzhlecaacauno87nq546.jpg "/>

ValueOf () is the method that Java calls to convert the base data type to the wrapper type, which is the ability to complete the automatic boxing.


The above code is very understood, that is to say: I in a certain interval, then returns an element of an array, otherwise the new integer returns!


Tracking code, you can see that Integercache.high is actually 127.


Then if the -128<=i<=127 from the dynamic boxing, the resulting array of elements, that is, pointing to the same piece of memory space!


Take a look at the test code:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/5C/wKiom1Xhg33BDFa8AAB3MhKqYCE168.jpg "title=" Sogou _ 2015-08-29_18-03-33.png "alt=" Wkiom1xhg33bdfa8aab3mhkqyce168.jpg "/>


A true, a false, is good to understand.


True, because i1,i2 points to the same thing in the cache array, not an integer can be compared with = =!


The integer also definitely overrides the Equals () method to achieve a value comparison:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/58/wKioL1XhhvnS96sSAAByypGX7Do835.jpg "title=" Sogou _ 2015-08-29_18-09-26.png "alt=" Wkiol1xhhvns96ssaabyypgx7do835.jpg "/>


Sentiment:


Even the integer has used the idea of caching!


Let's take a look at the Intvalue () method for automating the unpacking function:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/58/wKioL1XhiG_gtfzUAAAleeuTZpI405.jpg "title=" Sogou _ 2015-08-29_18-15-23.png "alt=" Wkiol1xhig_gtfzuaaaleeutzpi405.jpg "/>


Very simple, return value, what is this value?


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/58/wKioL1XhiJLBkSS4AAAfabitUX0809.jpg "title=" Sogou _ 2015-08-29_18-15-38.png "alt=" Wkiol1xhijlbkss4aaafabitux0809.jpg "/>

This means that once the integer is set, it cannot be changed! You can find that the integer also does not provide a set method.




Topic Two: Comparison of packaging types and basic types

Look at the code:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/5E/wKioL1XiUKfglgRSAABupEi9UIc677.jpg "title=" Sogou _ 2015-08-30_08-29-56.png "alt=" Wkiol1xiukfglgrsaabupei9uic677.jpg "/>


In the example above, the packing type and the basic type are converted into integers first when compared. or an int?


If it turns into an integer, then obviously one is false and one is true.


In fact, the result of the final operation is 2 is true.


This shows that in the comparison process, is the priority to the basic data type conversion!



Topic Three: Collections

In actual development, the collections and arrays are more than we use, and collections/arrays encapsulates a lot of features for us to call, and can meet most of our development needs, so understand what they are all very necessary!


Collections:


Sort function


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/5E/wKioL1XiU9nQw94oAABfBlxRhtk546.jpg "title=" Sogou _ 2015-08-30_08-43-36.png "alt=" Wkiol1xiu9nqw94oaabfblxrhtk546.jpg "/>


Specifically for list sorting, in actual development, we do often need to sort a list with some objects in it!



Min and Max


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/62/wKiom1XiUm3zU86CAACqBCF1SC0341.jpg "title=" Sogou _ 2015-08-30_08-46-40.png "alt=" Wkiom1xium3zu86caacqbcf1sc0341.jpg "/>


If we want to take a minimum and maximum value from a set, how do we get it quickly? The above method will help us to achieve.



Reverse


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/62/wKiom1XiU7uBm0qtAAAe_F_9gEU814.jpg "title=" Sogou _ 2015-08-30_08-52-31.png "alt=" Wkiom1xiu7ubm0qtaaae_f_9geu814.jpg "/>


If, we just need to get a list of the reverse order!



Mixed arrangement


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/62/wKiom1XiVsuCetCoAAAbtHoA7jM320.jpg "title=" Sogou _ 2015-08-30_09-05-33.png "alt=" Wkiom1xivsucetcoaaabthoa7jm320.jpg "/>


By this method, the order of the elements in the list is unpredictable, that is, the order is random and mixed.


What's the use of it?


For example, in some businesses, we want to provide the external data is "change", assuming that the e-commerce app, the user sees is always the same goods, and what are the interests of buying? The use of random features, so that users can see the same every time!



Copy


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/62/wKiom1XiWO_zMkcTAAA1TnibLUw385.jpg "title=" Sogou _ 2015-08-30_09-14-41.png "alt=" Wkiom1xiwo_zmkctaaa1tnibluw385.jpg "/>


Duplicate operations often, it should be noted that the length of the dest list needs to >=src list length, copy from Index=0, and is a shallow copy!



Find the index position of the first/last where the target list appears


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/5F/wKioL1XiX_miK-FHAAA7e7d0IHQ838.jpg "title=" Sogou _ 2015-08-30_09-35-44.png "alt=" Wkiol1xix_mik-fhaaa7e7d0ihq838.jpg "/>


We can take a look at how the source code is implemented:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/62/wKiom1XiXxTz3lMtAAM5zd0ZPR8371.jpg "title=" Sogou _ 2015-08-30_09-40-54.png "alt=" Wkiom1xixxtz3lmtaam5zd0zpr8371.jpg "/>


Description


If we do this function ourselves, how will we do it?


For finding the target List2 in the source List1, it is clear that the first step is to traverse List1, starting from index=0 to find out if each element corresponds to List2, and if not, repeat the process from index=1.


The above code, divided into ordinary for version and iterator version, the idea is consistent.


It is important to note that the continue in the inner for loop is not intended to start the next inner for loop, but rather to cause the outer for loop to start the next time, in order to achieve this, the continue lable is used;



Circular moving Rotate


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/62/wKiom1XiYwKzFhFFAAA9rSzFQXo492.jpg "title=" Sogou _ 2015-08-30_09-57-40.png "alt=" Wkiom1xiywkzfhffaaa9rszfqxo492.jpg "/>


If we want to move the first few in the list to the end, or the last few moves to the front, rotate can come in handy!


How did he make it? Seems like there's no clue? See how the source code is done:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/63/wKiom1XiambxxWrhAADB3iMXAHU358.jpg "title=" Sogou _ 2015-08-30_10-29-13.png "alt=" Wkiom1xiambxxwrhaadb3imxahu358.jpg "/>

The schematic is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/63/wKiom1XiayfBbevFAACPvpCuG-0267.jpg "title=" Sogou _ 2015-08-30_10-32-22.png "alt=" Wkiom1xiayfbbevfaacpvpcug-0267.jpg "/>


Just a few reverse to achieve, so simple!


Sentiment:


This means that some seemingly complex functions are assembled by a small number of features!




Topic Four: Arrays


The collection is good, but sometimes we just want to manipulate the basic type of data, then we may have an array, and arrays just provides some functionality for us to use, roughly as follows:


sorting function Sort:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/5F/wKioL1XicRXwXuNaAAD9mnPui6k863.jpg "title=" Sogou _ 2015-08-30_10-48-44.png "alt=" Wkiol1xicrxwxunaaad9mnpui6k863.jpg "/>


A lot of sort AH ~


Sort all elements of an array? Sort a range of elements of an array? Want to sort by the sort you've made?


Arrays.sort can satisfy you!



Array comparison function equals


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/60/wKioL1XiclbTdvU1AADFk6R7J_I735.jpg "title=" Sogou _ 2015-08-30_10-53-56.png "alt=" Wkiol1xiclbtdvu1aadfk6r7j_i735.jpg "/>


If we let ourselves compare the elements of the 2 arrays, then we have to determine the length, in the For loop, to compare the corresponding elements of the 2 arrays are equal, and now Arrays.equals has already encapsulated these logic, a line of code is OK ~


Can take a glance at his logic:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/63/wKiom1XicXazi0MNAADH87u01q8470.jpg "title=" Sogou _ 2015-08-30_10-59-17.png "alt=" Wkiom1xicxazi0mnaadh87u01q8470.jpg "/>


Sentiment:


Is it the same object? NULL check? Length judgment? Are you going to notice this before for comparison?



Finding array elements BinarySearch


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/60/wKioL1XidCqTq1sDAAEMmE5O_DQ468.jpg "title=" Sogou _ 2015-08-30_11-01-50.png "alt=" Wkiol1xidcqtq1sdaaemme5o_dq468.jpg "/>


This is the need for an ordered array to achieve binary lookups.


All element lookup? Partial element lookup? Specifies that the comparer is sorted after a lookup?


All can ~



Fill Array function fill



The ToString () function of the array


You know, sometimes when we debug the program, we often need to print the elements of the array, we have to write a for loop traversal, and arrays provides a convenient ToString () to return a comma separated list of elements.



Array copy function Copyof/copyofrange






This article is from the "Hard Struggle" blog, please make sure to keep this source http://zhangfengzhe.blog.51cto.com/8855103/1689732

Java Foundation Series -04-Basic type/package type/collections/arrays

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.