Java Container-arraylist

Source: Internet
Author: User

The concept of containers

  In Java, if there is a class that is designed to hold objects of other classes, this class is called a container, or a collection, which is a whole that is formed by combining several classes of objects of the same or similar nature.

Common methods in containers

1 intsize ();2 BooleanIsEmpty () non-null judgment3 Booleancontains (Object o) determines if o exists within the set4Iterator<e>iterator () set iterator5 object[] ToArray () returns an array containing all the elements in this container6 BooleanRemove (Object o) removes the O element from the collection7 BooleanAdd (Object obj) adds the specified element to the container8Object Get (intindex) Gets the element that is labeled Index9Object Remove (intindex) To delete the element labeled IndexTenObject Set (intindex,object Element) to place the subscript index OneObject Add (intindex,object Element) add an object element at the next index point A object put (object Key,object value) adds the specified element to the container -Object get (Object key) gets the keyword key

Container classification

The container can be divided into collection and map two kinds of interfaces. The list interface and the set interface inherit the Collection.

The implementation of the set interface for the HashSet and Treeset,list interfaces is implemented as HashMap and TreeMap for ArrayList and Linkedlist,map interfaces.

ArrayList Common methods

ArrayList implements a list interface, also known as a dynamic array, there are three kinds of constructors, the following are common methods and comments in the source code.

1 //constructor for the specified size2  PublicArrayList (intinitialcapacity) {3     if(Initialcapacity > 0) {4          This. Elementdata =Newobject[initialcapacity];5}Else if(initialcapacity = = 0) {6          This. Elementdata =Empty_elementdata;7}Else {8         Throw NewIllegalArgumentException ("Illegal capacity:" +9 initialcapacity);Ten     } One } A //parameterless Constructors -  PublicArrayList () { -      This. Elementdata =Defaultcapacity_empty_elementdata; the } - //Specifying content Constructors -  PublicArrayList (collection<?extendsE>c) { -Elementdata =C.toarray (); +     if((size = elementdata.length)! = 0) { -         if(Elementdata.getclass ()! = object[].class) +Elementdata = arrays.copyof (elementdata, size, object[].class); A}Else { at          This. Elementdata =Empty_elementdata; -     } - } -// return set length  -  Public intsize () { -     returnsize; in } - // Determine if there is a value in the set  to  Public BooleanIsEmpty () { +     returnSize = = 0; - } the // Determine if the object exists in the collection O  *  Public Booleancontains (Object o) { $     returnIndexOf (o) >= 0;Panax Notoginseng } - // Returns an array containing all the elements in this collection  the  Publicobject[] ToArray () { +     returnarrays.copyof (elementdata, size); A } the // Gets the element at the specified position  +  PublicE Get (intindex) { $     returnElementdata (index); - } - // replace element at specified position  the  PublicE Set (intindex, E Element) { theE OldValue =Elementdata (index); -Elementdata[index] =element; Wu     returnOldValue; - } About // add element  $  Public BooleanAdd (e e) { -   -elementdata[size++] =e; -     return true; A } + // add element at specified location  the  Public voidAddintindex, E Element) {
theElementdata[index] =element; -size++; in } the // Delete the element at the specified position the PublicE Remove (intindex) { About
theE OldValue =Elementdata (index); + - intnummoved = size-index-1; the if(nummoved > 0)BayiSystem.arraycopy (Elementdata, index+1, Elementdata, index, the nummoved); theElementdata[--size] =NULL; - - returnOldValue; the } the // in the specified element the Public BooleanRemove (Object o) { the if(O = =NULL) { - for(intindex = 0; index < size; index++) the if(Elementdata[index] = =NULL) { the Fastremove (index); the return true;94 } the}Else { the for(intindex = 0; index < size; index++) the if(O.equals (Elementdata[index])) {98 Fastremove (index); About return true; - }101 }102 return false;103 }104 the Private voidFastremove (intindex) {106modcount++;107 intnummoved = size-index-1;108 if(nummoved > 0)109System.arraycopy (Elementdata, index+1, Elementdata, index, the nummoved);111Elementdata[--size] =NULL; the}

ArrayList Array expansion

Whenever a method of adding elements, such as Add, AddRange, Insert, Insertrange, is checked, the capacity of the internal array is not sufficient, and if it is, it will reconstruct an array at twice times the current capacity, copy the old elements into the new array, and discard the old array. At this point in the expansion operation, it should be compared to the impact of efficiency.

ArrayList use case

1  Public classArraylistuse {2      Public Static voidMain (string[] args) {3List test =NewArrayList ();4 5Test.add ("1");6Test.add ("2");7Test.add ("3");8Test.add ("4");9Test.add ("5");Ten  OneTest.add (0,5); ASYSTEM.OUT.PRINTLN ("Specify Location Add:" +test.get (0)); -SYSTEM.OUT.PRINTLN ("Specify Location Add:" +test.get (1)); -  theTest.remove (2); -SYSTEM.OUT.PRINTLN ("Specify Location Delete" +test.get (2)); -System.out.println ("Length" +test.size ()); -  +  -     } +}

Summarize

ArrayList is a collection class implemented with arrays and is non-thread-safe, so it is recommended that you use ArrayList in a single thread, and you can select vectors or copyonwritearraylist in multiple threads.

If there is something wrong, please leave a message!

Java Container-arraylist

Related Article

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.