The previous blog introduced dictionary, this blog introduces the List of related content.
C # to store a set of data, we think of the array array,arraylist,list these three objects, where arrays are the first to appear, let's start with the array.
Array has many advantages, the use of arrays is relatively simple, and the array in memory is continuous storage, so the data access speed is relatively fast.
Define an array of arrays :
// definition, assignment
<span style= "FONT-SIZE:18PX;" > int[] inttest = new Int[3]; Inttest[0] = 1; INTTEST[1] = 2; INTTEST[2] = 3; Modify inttest[1] = 5;</span>
However, the array has a lot of flaws, for example, its length is a big pit, we must specify the array length when defining the array, if the length is too long to waste memory, if too short, will compile errors, overflow phenomenon, if we do not know the length of the array beforehand, That's a pit-father. Also, data cannot be deleted, and inserting a piece of data is quite difficult.
c# launched arraylist arraylist arraylist is System.Collections; The collection class under namespace, we define the arraylist
ArrayList inherited the IList interface, you can easily add data, modify, insert, delete operations. The code is as follows:
<span style= "White-space:pre" ></span> //definition, adding data ArrayList ArrayList = new ArrayList (); Arraylist.add (1); Arraylist.add (2); Arraylist.add (3); Modify arraylist[0] = "5"; Delete arraylist.removeat (0); Inserting data arraylist.insert (0, 555);
ArrayListstorage structure is the same as the storage structure of the array, is also a continuous storage, so the data query and add faster, but the deletion speed is relatively slow, because the continuous storage of data changes one of the records, you need to change the entire array index, this is more troublesome.
ArrayListIt can be said that the defect of the array is solved well, but it is not perfect. When you look at the code above, you will find that the first index value we give to the instance ArrayList object isinttype of1, but when we modified the data, we changed thestringtype of5, that is to say, arrayList this object, bothinttype data, also includesstringtype data, do we have to judge every time we use it? What if we added several more types? The program will definitely error. If said, we are in the process of making countless judgments, but the system of boxing, unpacking operation can also be tortured to death, and, memory is a great consumption.
c#2.0 The advent of generics is a good solution to this problem. the type safety of arraylist poses a great risk to the program,and list requires explicit data types at the time of definition, so thelist is a strongly typed collection, and the advent of generics avoids data exchange and storage of fashion boxes and unpacking operations, greatly reducing memory loss. Its usage and ArrayList are basically the same. The code is as follows:
<span style= "White-space:pre" ></span> //definition, adding data list<string> listtest = new list< String> (); Listtest.add ("a"); Listtest.add ("B"); Listtest.add ("C"); Modify listtest[1] = "abc"; Removal of listtest.removeat (0); Insert Data listtest.insert (0, "Hello world!");
Summary:
Arrays: Arrays can store data in multiple dimensions, stored in the sequential table structure in memory, indexed by the table below to find data, query efficiency is high, data operation is convenient. However, the length is fixed, data insertion is difficult, and is recommended if you need to store it in a multidimensional data structure.
ArrayList: the enhanced version of the array, the length can be changed, in-memory sequential table structure storage, through the index to find data, query efficiency, inherit the IList interface, data operation is simple. Type is unsafe, data query and storage need to be boxed and unboxing operation, memory waste is large, is not recommended to use.
List : ArrayList generic version, strongly typed data collection, data manipulation is simple, in-memory chain storage structure exists, you can query data by index, query efficiency is high, Operation General collection is recommended.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Container Use Notes (List article)