In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Many novices are not very clear about how to use ArrayList in C#. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
The ArrayList class is a special array. By adding and removing elements, you can dynamically change the length of the array.
First, advantages
1. Support for automatic resizing
two。 You can insert elements flexibly.
3. You can delete elements flexibly.
4. Flexible access to elements
II. Limitations
The speed is lower than that of an ordinary array.
In the words of Microsoft:
"any reference or value type added to ArrayList will be implicitly cast up to Object. If the item is a value type, it must be boxed when it is added to the list and unboxed when retrieved. Casting as well as boxing and unboxing operations degrade performance; the impact of boxing and unboxing is obvious when large collections must be looped."
ArrayList inheritance diagram
Third, add elements
1.public virtual int Add (object value)
Add an object to the end of the ArrayList
ArrayList aList=new ArrayList ()
AList.Add ("a")
AList.Add ("b")
AList.Add ("c")
AList.Add ("d")
AList.Add ("e")
The content is abcde
2.public virtual void Insert (int index,object value)
Inserts an element at the specified index of the ArrayList
AList.Insert (0, "aa")
The result is aaabcde
3.public virtual void InsertRange (int index,ICollectionc)
Inserts an element in the collection at the specified index of the ArrayList
ArrayList list2=new ArrayList ()
List2.Add ("tt")
List2.Add ("ttt")
AList.InsertRange (2Jing List2)
The result is abtttttcde
IV. Delete
A) public virtual void Remove (object obj)
Remove the first occurrence of a specific object from the ArrayList, note that it is the first
AList.Remove ("a")
The result is bcde
2.public virtual void RemoveAt (intindex)
Remove the element at the specified index of the ArrayList
AList.RemoveAt (0)
The result is bcde
3.public virtual void RemoveRange (int index,int count)
Removes a range of elements from the ArrayList. Index represents the index, and count represents the number starting at the index
AList.RemoveRange (1pr 3)
The result is ae
4.public virtual void Clear ()
Removes all elements from the ArrayList.
5. Sorting
A) public virtual void Sort ()
Sorts the elements in the ArrayList or part of it.
DropDownList1.DataSource=aList;//DropDown ListDropDownList1
DropDownList1.DataBind ()
The result is eabcd
AList.Sort (); / / sort
DropDownList1.DataSource=aList;//DropDownListDropDownList1
DropDownList1.DataBind ()
The result is abcde
B) public virtual void Reverse ()
Reverses the order of elements in an ArrayList or part of it.
AList.Reverse (); / / reverse
DropDownList1.DataSource=aList;//DropDownListDropDownList1
DropDownList1.DataBind ()
The result is edcba
VI. Search
A) public virtual int IndexOf (object)
B) public virtual int IndexOf (object,int)
C) public virtual int IndexOf (object,int,int)
Returns the zero-based index of the first match of a value in ArrayList or part of it. No return-1 was found.
IntnIndex=aList.IndexOf ("a"); / / 1
NIndex=aList.IndexOf ("p"); / / not found,-1
D) public virtual int LastIndexOf (object)
E) public virtual int LastIndexOf (object,int)
F) public virtual int LastIndexOf (object,int,int)
Returns the zero-based index of the last match of a value in ArrayList or part of it.
IntnIndex=aList.LastIndexOf ("a"); / / the value is 2 instead of 0
G) public virtual bool Contains (objectitem)
Determines whether an element is in the ArrayList. Include return true, otherwise return false
Get the elements in the array
Here is a way to get the value of an element by taking an integer as an example
ArrayList aList=new ArrayList ()
For (int iTuno Bandi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.