Http://anony3721.blog.163.com/blog/static/5119742010728102932277/
1. What is arraylist?
Arraylist is the legendary dynamic array. In msdn, It is the complex version of array. It provides the following benefits:
The icollection and ilist interfaces are implemented to dynamically add and remove elements, and the array size is flexibly set.
2. How to Use arraylist
The simplest example:
Arraylist list = new arraylist ();
For (INT I = 0; I <10; I ++) // Add 10 int elements to the array list. Add (I );
//... The program does some processing
List. removeat (5); // remove the 6th Elements
For (INT I = 0; I <3; I ++) // Add three more elements.
List. Add (I + 20 );
Int32 [] values = (int32 []) List. toarray (typeof (int32); // returns the array contained in arraylist
This is a simple example. Although it does not contain all the methods of arraylist, it can reflect the most common usage of arraylist.
C # dynamic arrays are not supported. arraylist can be used to implement dynamic arrays.
The namespace of arraylist is system. Collections.
Arraylist element type
Different from arrays, the types of each element in arraylist can be different.
Object Declaration
// Three methods for declaring arraylist overloading are available, and two methods are commonly used.
Arraylist Al = new arraylist ();
Arraylist Al = new arraylist (3 );
In the preceding example, parameter value 3 indicates the capacity, that is, the number of elements that can be accommodated.
The arraylist object is a variable-length array. You can add elements as needed. you can use the arraylist method to add or retrieve elements to the array list and modify an element. for example, arraylist myarraylist = new arraylist (); myarraylist. add ("caoxi"); myarraylist. clear ();
All objects retrieved from arraylist are of the object type. Before using arraylist, convert them to an appropriate type. arraylist thearraylist = new arraylist (); thearraylist. add ("1"); thearraylist. add ("2"); string S = (string) thearraylist [0]; string S1 = (string) thearraylist [1]; pay attention to contains () if arraylist contains objects provided by parameters, true is returned. Otherwise, falseif (thearraylist. contains ("1") // determines whether the character "1" exists in the arraylist objects ---------------------------------------------------------------------------------------------
In C #, the set includes the commonly used C # arraylist (dynamic array), hashtable (search table for key words and values), and The infrequently used bitarray (Bit Array ), queue, sortedlist, and stack.
In fact, a set is a combination of ordered data that can be effectively processed. Here we will mainly introduce commonly used C # arraylist and hashtable.
Arraylist
Similar to a one-dimensional dynamic array, arraylist can store any object. There are three common methods for arraylist: add (), insert (), and remove ().
- Using system. collections;
- Public static void main ()
- {
- Arraylist arr = new arraylist ();
- Arr. Add (10); // Add a value for the set
- Arr. Add (10); // Add the second value
- Arr. insert (0, 8); // insert a value of 8 at the index position of 0th
- Console. writeline (ARR. indexof (10, 2 ));
- // Search refers to the number of 10 index values from 0 to 2.
- Foreach (int A in ARR) // traverses the set arr
- {
- Console. writeline ();
- }
-
- }
Hashtable
Is used to store a set of key/value pairs. If you need to store keys at the same time and have corresponding values, you can use hashtable.