2012年3月21日星期三

In Ear HeadphonesProblem put forwardgives two points in time ( e

Problem put forwardgives two points in time ( e.g. ,2006032120080402) ,require the calculation of out of the middle of the all dates, and displayed .At the point of the call to take a container to hold these dates stored .
However ,due to the time point is randomly given ,can not determine the number of elements in an array .Therefore ,the use of ArrayList .The use of ArrayList ZZ 1 ,what is the ArrayList ArrayList dynamic array ,with the MSDN statement, Array is the complex version ,it provides the following benefits: the dynamics of increase and decrease of ICollection and IList interface element to realize the flexible setting the size of the array 2 ,how to use the ArrayList simple example: ArrayList List = new ArrayList ( ) ;for ( int i = 0 ;i< 10 ;i& 43 ;# ;& # 43 ;) / / to the array increases 10 Int elements List.
Add ( I ) ;/ / ..program does some processing List.RemoveAt ( 5) ;/ / the sixth elements to remove the for ( int i = 0 ;i< 3 ;i& 43 ;# ;& # 43 ) ;/ / add a 3 element List.
Add ( i& # 43 ;20) ;Int32 values = ( Int32 ) List.ToArray ( typeof ( Int32 ) ) ;/ / returns ArrayList contains the array which is a simple for example ,although did not include ArrayList all the way ,but can reflect the most common use of ArrayList 3 ,ArrayList important methods and attributes 1) constructor ArrayList provides three constructors :public ArrayList ( ) ;the default constructor ,will be the default (16 ) size to initialize internal array ( ICollecti public ArrayList On ) ;a ICollection objects to construct ,and the set of elements that are added to the ArrayList public ArrayList ( int ) ;with the specified size to initialize internal array 2 ) IsSynchronized attribute and the ArrayList.
Synchronized method IsSynchronized property indicates the current instance of ArrayList does not support thread synchronization ,and the static ArrayList.Synchronized method returns a ArrayList thread synchronization package .
If the use of thread synchronization examples, then in the access of multiple threads of time ,need to manually call lock to maintain synchronization of threads ,for example :ArrayList list = new ArrayList ( ) ;/ /.
.. Lock ( list.SyncRoot ) / / when ArrayList is a thread when packing, the SyncRoot attribute is its own ,but in order to meet ICollection SyncRoot definition ,here is the use of SyncRoot to maintain the source code of the normative {list.
Add ( Add a Item ) ;} if you use the ArrayList.Synchronized method returns the instance ,then do not consider the thread synchronization issues ,this example is thread safe ,actually ArrayList internal implementation of a guarantee of the internal thread synchronization class ,ArrayList.
Synchronized returned is an instance of this class ,which each attribute is used lock keywords to ensure thread synchronization .3) Count and Capacity attributes Count property is the ArrayList contains a number of elements ,this property is read-only .
The Capacity property is the ArrayList can contain the largest number ,can be manually set this property, but when the set is less than 20540 Count& # ;when an exception is thrown .4) Add ,AddRange ,Remove ,RemoveAt ,RemoveRange ,Insert ,InsertRange these methods # & 20284 ;Add method is used to add an element to the current list at the end of the AddRange method is used to add a number of elements to the current list at the end of the Remove method is used to delete an element by element itself ,a reference to the deletion of RemoveAt method for deleting an element ,through the index & # 20540 ;to delete the RemoveRange used to remove a number of elements ,by specifying start indexing and delete to delete the Insert number used to add an element to a specified location ,list elements were moving back behind InsertRange for starting at the specified position to add a number of elements ,list behind elements were moving back in addition ,there are several types of & # 20284 ;methods :Clear method for the removal of all existing elements Contains method used to locate an object in the list the other I will not one one cumbersome, you can view the MSDN ,it more carefully 5 ) TrimSize method this method is used to ArrayList fixed to the actual element size ,when the dynamic array elements identified in tim With time, you can call this method to release free memory .
6) ToArray method this method ArrayList Copy element into a new array .In 4 ,ArrayList and array example 1: ArrayList List = new ArrayList ( ) ;List.Add ( 1) ;List.Add ( List.Add 2 ) ;( 3 ) ;Int32 values = ( Int32 List.
ToArray ( typeof ) ( Int32 ) ) ;case 2 : ArrayList List = new ArrayList ( ) ;List.Add ( 1 ) ;List.Add ( List.Add 2 ) ;( 3 ) ;Int32 values = new Int32 ;List.CopyTo ( values ) ;it introduces two kinds of transformation from ArrayList to array method case 3 : ArrayList List = new ArrayList ( ) ;List.
Add ( string ) ;List.Add ( 1) ;/ to add different types of elements in the array object values = List.ToArray ( typeof ( object ) ) ;/ / String values = ( string List.
ToArray ( typeof ) ( string ) ) ;/ / error and the array are not the same, because can be converted to a Object array, so to add different ArrayList type element is not wrong ,but when calling the ArrayList method, or transfer of all elements can correct transition type or Object type ,Coach Outlet Online,otherwise it will throw the exception to transformation .
In 5 ,ArrayList best recommendations in this section we discuss the difference between ArrayList and array ,and ArrayList efficiency 1) ArrayList is Array complex versions of ArrayList encapsulate an array of type Object ,Monster Crossfade,from a general sense,Moncler Coats, it and the array is not fundamentally different ,and even many ArrayList methods ,such as Index ,IndexOf ,Contains ,Sort etc is in an internal array based on direct calls to the Array correspondence method .
2) the internal Object type effects for general reference types ,this part of the impact is not great, but for & # 20540 ;type ArrayList ,to add and modify the elements ,will cause the boxing and unboxing operation ,frequent operation may affect the efficiency of a part .
But just for the majority of people ,most of the applications are using & # 20540 ;type of array .To eliminate this effect is no way ,unless you do not use it, or to assume part of the efficiency loss ,In Ear Headphones,but the loss of this part will not be great .
3) array expansion which is on the ArrayList efficiency comparison of the effects of a factor .Whenever the execution Add ,AddRange ,Insert ,InsertRange, add the element method ,will inspect the internal array capacity is not enough, if it is ,it will be to the current capacity of two times to construct an array ,the old elements of Copy to the new array, then discard the old array ,in this critical point of expansion operations ,should be affect the efficiency of .
Example 1 :for example ,one may have a 200 element data dynamically added to a default of 16 element size created ArrayList ,will pass through :16 * 2 * 2 * 2 * 2 = 256 four expansion will meet the requirements ,so if one begins with :ArrayList List = new ArrayList ( 210) ;the creation of ArrayList ,not only will reduce the 4 array creation and operation of Copy ,will reduce the memory usage .
Example 2 :an estimated 30 elements to create an ArrayList : ArrayList List = new ArrayList ( 30) ;in the process of implementation ,the joining of the 31 elements ,then the array will be expanded to a 60 element size ,and this time there is no new elements added ,and there is a call to the TrimSize method ,then the there was a 1 expansion of the operation ,and wasted 29 element size space .
If this time ,Monster Store,use :ArrayList List = new ArrayList ( 40) ;then all settled .Therefore ,the correct prediction of possible elements ,and at an appropriate time to call the TrimSize method is used to improve ArrayList efficiency of the important ways .
4) frequent calls IndexOf ,Contains methods ( Sort ,BinarySearch methods after optimization, not listed here ) caused by loss of efficiency first, we must be clear ,ArrayList is a dynamic array ,it does not include the Key or Value quick access algorithm ,so the fact that calls IndexOf ,Contains method is performed by simple the cycle to find elements ,so frequent call such a method is not better than your own write cycle and some optimization to the quick, if have this requirement ,we recommend using the Hashtable or SortedList key & # 20540 ;pairs set .
ArrayList al = new ArrayList ( ) ;al.Add ( " ;How" ;al.Add ) ;( " ;are" ;al.Add ) ;( " ;" ;al.Add ;you !) ( 100) ;al.Add ( al.Add 200 ) ;( 300 ) ;al.
Add ( al.Add 1.2 ) ;( 22.8) ;/ / ... The first traversal method of the ArrayList object foreach ( object o in Al ) {Console.Write ( o.ToString ( 43 ) & # ;" ;" ;) ;} / / second traverse method of the ArrayList object IEnumerator ie = al.
GetEnumerator ( ) ;while ( ie.MoveNext ( ) ) {Console.Write ( ie.Curret.ToString ( " ) & # 43 ;;" ;) ;} / / third traverse method of the ArrayList object ( ArrayList.size ) ;and then in the use of index for ( int i = 0 ;i< ;Count ;i& # 43 & 43 ;# ;) {Console.
Related articles:

没有评论:

发表评论