Basic use of Java basic----> arrays (i)

Source: Internet
Author: User
Tags concurrentmodificationexception

Arrays are the most efficient way to store and randomly access an object's reference sequence, and we present a simple introduction to arrays today.

Simple use of arrays one, the assignment of arrays
New string[]{"Linux", "Huhx", "Android"}; //  //  array out of string[] Arrays2 = arrays;arrays2[//  Modify the value of Arrays2, Impact on Arrays //  [Linux, Chenhui, Android]arrays[2] = "Google"//  [ Linux, Chenhui, Google]

Second, deep copy of the array
// the bottom implementation is still the System.arrarycopy () method // [Linux, Chenhui, Google] // Modify the value of the ARRAYS3 without affecting the arrays // [Linux, Chenhui, Google]

Three, the padding of the array
New String[4"Linux"//  [Linux , Linux, Linux, Linux]

Iv. Comparison of array contents
boolean arraysequal =//  false//  true

The ordering of the array, changing the array itself
// [Chenhui, Google, Linux]

Vi. searching for elements within an array
int location = Arrays.binarysearch (Arrays, "Linux"//  2

Seven, Array generation ArrayList
list<string> strings = arrays.aslist (Arrays); Iterator<String> Iterator = strings.iterator () ;  while (Iterator.hasnext ()) {    System.out.print (Iterator.next ());}

Analysis of the Arrays class method

First, fill filling method

 Public Static void Fill (object[] A, Object val) {    for (int i = 0, len = a.length; i < len; i++)        
    = val;}

Two, ToString printing method

 Public StaticString toString (object[] a) {if(A = =NULL)        return"NULL"; intIMax = a.length-1; if(IMax = =-1)        return"[]"; StringBuilder b=NewStringBuilder (); B.append (‘[‘);  for(inti = 0;; i++) {b.append (string.valueof (a[i)); if(i = =IMax)returnB.append ('] '). toString (); B.append (", "); }}

Three, equal comparison method

 Public Static Booleanequals (object[] A, object[] A2) {if(a==A2)return true; if(a==NULL|| a2==NULL)        return false; intLength =a.length; if(A2.length! =length)return false;  for(inti=0; i<length; i++) {Object O1=A[i]; Object O2=A2[i]; if(! (o1==NULL? o2==NULL: O1.equals (O2))) return false; }    return true;}

Iv. ways to convert aslist into lists

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

Five, iterator traversal method

 PublicIterator<e>iterator () {return NewItr ();}/*** An optimized version of Abstractlist.itr*/Private classItrImplementsIterator<e> {    intCursor//index of next element to return    intLastret =-1;//index of last element returned;-1 if no such    intExpectedmodcount =Modcount;  Public BooleanHasnext () {returnCursor! =size; } @SuppressWarnings ("Unchecked")     PublicE Next () {checkforcomodification ();//check consistency of array contents        inti =cursor; if(I >=size)Throw Newnosuchelementexception (); Object[] Elementdata= ArrayList. This. Elementdata; if(I >=elementdata.length)Throw Newconcurrentmodificationexception (); Cursor= i + 1; return(E) Elementdata[lastret =i]; }     Public voidRemove () {if(Lastret < 0)            Throw Newillegalstateexception ();        Checkforcomodification (); Try{ArrayList. This. Remove (Lastret); Cursor=Lastret; Lastret=-1; Expectedmodcount=Modcount; } Catch(Indexoutofboundsexception ex) {Throw Newconcurrentmodificationexception (); }    }    Final voidcheckforcomodification () {if(Modcount! =expectedmodcount)Throw Newconcurrentmodificationexception (); }}

Friendship Link

Basic use of Java foundation----> Arrays (i)

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.