In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Namespace: System.Collections.Generic
Public class List: IList, ICollection, IEnumerable, IList, ICollection, IEnumerable
The List class is the generic equivalent of the ArrayList class. This class implements the IList generic interface using an array whose size can be dynamically increased as needed.
The benefits of generics: it adds great effectiveness and flexibility to writing object-oriented programs in the c # language. There is no forced boxing and unboxing of value types, or downward casting of reference types, so performance is improved.
Performance considerations:
When deciding whether to use the IList or the ArrayList class (both have similar functionality), keep in mind that the IList class performs better and is type-safe in most cases.
If you use a reference type for type T of the IList class, the behavior of the two classes is exactly the same. However, if you use a value type for type T, you need to consider implementation and boxing issues.
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."
1. The basis and common methods of List:
Declaration:
1. List mList = new List ()
T is the element type in the list, now take the string type as an example
List mList = new List ()
2. List testList = new List (IEnumerable collection)
Create a List with a collection as a parameter
String [] temArr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu"}
List testList = new List (temArr)
Add elements:
1 、 List. Add (T item) add an element
MList.Add ("John")
2 、 List. AddRange (IEnumerable collection) adds a set of elements
String [] temArr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu"}
MList.AddRange (temArr)
3. Insert (int index, T item); add an element in the index location
MList.Insert (1, "Hei")
Traverse the elements in the List:
The type of foreach (T element in mList) T is the same as when mList declares it
{
Console.WriteLine (element)
}
Foreach (string s in mList)
{
Console.WriteLine (s)
}
Delete the element:
1 、 List. Remove (T item) deletes a value
MList.Remove ("Hunter")
2 、 List. RemoveAt (int index); delete the element with the subscript index
MList.RemoveAt (0)
3 、 List. RemoveRange (int index, int count)
Starting with the subscript index, delete the count elements
MList.RemoveRange (3,2)
Determine whether an element is in the List:
List. Contains (T item) returns true or false, which is very practical
Sort the elements in the List:
List. Sort () defaults to the ascending order of the first letter of the element
MList.Sort ()
Reverse the order of elements in List:
List. Reverse () can be used with List. Sort () is used together to achieve the desired effect.
MList.Sort ()
List clearance: List. Clear ()
MList.Clear ()
Get the number of elements in List:
List. Count () returns an int value
Int count = mList.Count ()
Console.WriteLine ("The num of elements in the list:" + count)
2. Advanced and powerful methods of List:
List for example:
String [] temArr = {Ha "," Hunter "," Tom "," Lily "," Jay "," Jim "," Kuku "," Locu "}
MList.AddRange (temArr)
The List.Find method: searches for elements that match the criteria defined by the specified predicate and returns the first matching element in the entire List.
Public T Find (Predicate match)
Predicate is a delegate to a method that returns true if the object passed to it matches the condition defined in the delegate. The elements of the current List are passed one by one to the Predicate delegate and move forward in List, starting with the first element and ending with the last element. Processing stops when a match is found.
Predicate can be delegated to a function or a Ramda expression
Delegate to Ramda expression:
Delegate to a function:
String listFind1 = mList.Find (ListFind); / / delegate to the ListFind function
Console.WriteLine (listFind); / / output is Hunter
ListFind function:
Public bool ListFind (string name)
{
If (name.Length > 3)
{
Return true
}
Return false
}
The results of the two methods are the same.
The List.FindLast method: searches for elements that match the criteria defined by the specified predicate and returns the last matching element in the entire List.
Public T FindLast (Predicate match)
The usage is the same as List.Find.
The List.TrueForAll method: determines whether each element in the List matches the condition defined by the specified predicate.
Public bool TrueForAll (Predicate match)
Delegate to Ramda expression:
Bool flag = mList.TrueForAll (name = >
{
If (name.Length > 3)
{
Return true
}
Else
{
Return false
}
}
);
Console.WriteLine ("True for all:" + flag); / / the value is false
Delegate to a function, where the above ListFind function is used:
Bool flag = mList.TrueForAll (ListFind); / / delegate to the ListFind function
Console.WriteLine ("True for all:" + flag); / / the value is false
The results of the two methods are the same.
The List.FindAll method: retrieves all elements that match the conditions defined by the specified predicate.
Public List FindAll (Predicate match)
List subList = mList.FindAll (ListFind); / / delegate to the ListFind function
Foreach (string s in subList)
{
Console.WriteLine ("element in subList:" + s)
}
At this point, subList stores all elements with a length greater than 3.
List.Take (n): the type returned by the first n lines of IEnumetable,T is the same as the type of List
IEnumerable takeList= mList.Take (5)
Foreach (string s in takeList)
{
Console.WriteLine ("element in takeList:" + s)
}
At this point, the elements stored in takeList are the first five elements in mList.
The List.Where method: retrieves all elements that match the conditions defined by the specified predicate. Similar to the List.FindAll method.
IEnumerable whereList = mList.Where (name = >
{
If (name.Length > 3)
{
Return true
}
Else
{
Return false
}
});
Foreach (string s in subList)
{
Console.WriteLine ("element in subList:" + s)
}
At this point, subList stores all elements with a length greater than 3.
The List.RemoveAll method removes all elements that match the conditions defined by the specified predicate.
Public int RemoveAll (Predicate match)
MList.RemoveAll (name = >
{
If (name.Length > 3)
{
Return true
}
Else
{
Return false
}
});
Foreach (string s in mList)
{
Console.WriteLine ("element in mList:" + s)
}
At this point, mList stores elements whose length is greater than 3.
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.