Basics of Java Basic----> arrays (ii)

Source: Internet
Author: User

Here are some simple analysis of some of the methods in the list to avoid misuse of some functions. handwritten yao Note by the rain, fuzzy point painting fee to explore, although the word in the book, Hard to Kill a lover heart.

Method One, Arrays.aslist (T...arrays) method noted in list
@Test  Public void listaslists () {    List<String> strings = Arrays.aslist ("Huhx", "Linux", "liuling");    System.out.println (Strings.size ());    Strings.add (//  will report abnormal     System.out.println (strings);}

This is the case with the list returned by Arrays.aslist, which is illegal to modify, delete, and add to the list, which is to throw an exception. First we look at the source code:

@SafeVarargs @suppresswarnings ("varargs")  Public static <T> list<t> aslist (T ... a) {    returnnew arraylist<> (a);}

It is very important to note that the ArrayList in this is not our common java.util inside the ArrayList, it is arrays inside of an inner class. But he is the same as the ArrayList in the Java.util package, inheriting from the Abstractlist<e> class.

Private Static class extends Implements randomaccess, java.io.Serializable {        privatestaticfinallong Serialversionuid = -2764017481108945198l;         Private Final e[] A;}

For this generated ArrayList, there is no rewriting of the add, remove, and set methods of the parent class. So we call the Add method above and actually call the parent class Abstractlist's Add method. For the methods of these operations lists, Abstractlist throws a Unsupportedoperationexceptionn exception. The code is as follows:

 public  E Set (int   index, E element) { throw  new  Span style= "color: #000000;" > Unsupportedoperationexception ();}  public  void  Add (int   index, E Element) { throw  new   Unsupportedoperationexception ();}  public  E Remove (int   index) { throw  new   Unsupportedoperationexception ();}  

For the genuine ArrayList, that is, the Java.util package below. Because it overrides these methods of operation, it will naturally not throw an exception.

Second, List conversion array method ToArray ()
@Test Public voidlistasarrays () {List<String> lists =NewArraylist<>(); Lists.add ("Linux"); Lists.add ("Huhx"); //size = lengthstring[] Arrays =Newstring[lists.size ()]; String[] Strings=Lists.toarray (arrays); System.out.println (arrays.tostring (Arrays)+arrays.tostring (strings)); //size > Lengthstring[] Arrays1 =NewString[1]; String[] STRINGS1=Lists.toarray (ARRAYS1); System.out.println (arrays.tostring (arrays1)+ arrays.tostring (STRINGS1));//Size < length//Size < lengthstring[] Arrays2 =NewString[3]; String[] Strings2=Lists.toarray (ARRAYS2); System.out.println (arrays.tostring (ARRAYS2)+arrays.tostring (STRINGS2));}

The results of the program output are as follows:

[Linux, Huhx][linux, huhx][nullnull NULL]

Let's take a look at the source code of ToArray, which is the ArrayList in the Java.util bag. It is actually using the underlying array copy.

 public <T> t[] ToArray (t[] a) {    if (A.length < size)        // Make a new array of A's runtime type, but my contents:        return (t[]) arrays.copyof (elementdata, size, A.getclass ());     0, A, 0, size);     if (A.length > size)         NULL ;     return A;}

Friendship Link

Basics of Java Basic----> arrays (ii)

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.