Array类提供了用于创建,操作,搜索和排序数组的方法。 Array类不是System.Collections命名空间的一部分,但由于它基于IList接口,因此仍被视为一个集合。 Array类是支持数组的语言实现的基类。
数组类的特征:
- 在Array中,元素是数组的值,而数组的长度是数组中存在的项的总数。
- 数组的下界是其第一个元素的索引,下界的默认值为0。
- 数组的默认大小为2GB 。
- 具有相同数组类型的数组对象共享相同的Type对象。
例子:
// C# program to creating an array
// of the string as coffee name, store
// coffee name in the store,
// and prints each value.
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declares an 1D Array of string
string[] store;
// allocating memory for coffee names.
store = new string[] {"Americano, ", "Cafe au lait, ",
"Espresso, ", "Cappuccino, ",
"Long Black, ", "Macchiato" };
// Displaying Elements of the array
Console.WriteLine("Different types of coffee: ");
Console.WriteLine();
foreach(string coffeename in store)
Console.WriteLine(coffeename + " ");
}
}
}
输出:
Different types of coffee:
Americano,
Cafe au lait,
Espresso,
Cappuccino,
Long Black,
Macchiato
特性
Property | Description |
---|---|
IsFixedSize | Gets a value indicating whether the Array has a fixed size. |
IsReadOnly | Gets a value indicating whether the Array is read-only. |
IsSynchronized | Gets a value indicating whether access to the Array is synchronized (thread safe). |
Length | Gets the total number of elements in all the dimensions of the Array. |
LongLength | Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array. |
Rank | Gets the rank (number of dimensions) of the Array. For example, a one-dimensional array returns 1, a two-dimensional array returns 2, and so on. |
SyncRoot | Gets an object that can be used to synchronize access to the Array. |
范例1:
// C# program to illustrate
// Length property of Array class
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declares an 1D Array of string.
string[] topic;
// allocating memory for topic.
topic = new string[] {"Array, ", "String, ",
"Stack, ", "Queue, ",
"Exception, ", "Operators"};
// Displaying Elements of the array
Console.WriteLine("Topic of C#:");
Console.WriteLine();
// Here we calculate and print
// the length of the array, i.e. 6
Console.WriteLine("Length of the array: {0}",
topic.Length);
foreach(string ele in topic)
Console.WriteLine(ele + " ");
}
}
}
输出:
Topic of C#:
Length of the array: 6
Array,
String,
Stack,
Queue,
Exception,
Operators
范例2:
// C# program to illustrate the
// Rank property of Array class
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declares an 1D Array of string.
string[] topic;
// allocating memory for topic.
topic = new string[] {"Array, ", "String, ",
"Stack, ", "Queue, ",
"Exception, ", "Operators" };
// Displaying Elements of array
Console.WriteLine("Topic of C#:");
Console.WriteLine();
// Rank property provides the dimension rank
// here we use 1-D array so it return 1
// if we use 2-D array then it will return 2
Console.WriteLine("Rank of the array: {0}",
topic.Rank);
foreach(string ele in topic)
Console.WriteLine(ele + " ");
}
}
}
输出:
Topic of C#:
Rank of the array: 1
Array,
String,
Stack,
Queue,
Exception,
Operators
方法
Method | Description |
---|---|
AsReadOnly() | Returns a read-only wrapper for the specified array. |
BinarySearch() | Searches a one-dimensional sorted Array for a value, using a binary search algorithm. |
Clear() | Sets a range of elements in an array to the default value of each element type. |
Clone() | Creates a shallow copy of the Array. |
ConstrainedCopy() | Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely. |
ConvertAll() | Converts an array of one type to an array of another type. |
Copy() | Copies a range of elements in one Array to another Array and performs type casting and boxing as required. |
CopyTo() | Copies all the elements of the current one-dimensional array to the specified one-dimensional array. |
CreateInstance() | Initializes a new instance of the Array class. |
Empty() | Returns an empty array. |
Equals() | Determines whether the specified object is equal to the current object. |
Exists() | Determines whether the specified array contains elements that match the conditions defined by the specified predicate. |
Find() | Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array. |
FindAll() | Retrieves all the elements that match the conditions defined by the specified predicate. |
FindIndex() | Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within an Array or a portion of it. |
FindLast() | Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire Array. |
FindLastIndex() | Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within an Array or a portion of it. |
ForEach() | Performs the specified action on each element of the specified array. |
GetEnumerator() | Returns an IEnumerator for the Array. |
GetHashCode() | Serves as the default hash function. |
GetLength() | Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array. |
GetLongLength() | Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array. |
GetLowerBound() | Gets the index of the first element of the specified dimension in the array. |
GetType() | Gets the Type of the current instance. |
GetUpperBound() | Gets the index of the last element of the specified dimension in the array. |
GetValue() | Gets the value of the specified element in the current Array. |
IndexOf() | Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array. |
Initialize() | Initializes every element of the value-type Array by calling the default constructor of the value type. |
LastIndexOf() | Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Resize() | Changes the number of elements of a one-dimensional array to the specified new size. |
Reverse() | Reverses the order of the elements in a one-dimensional Array or in a portion of the Array. |
SetValue() | Sets the specified element in the current Array to the specified value. |
Sort() | Sorts the elements in a one-dimensional array. |
ToString() | Returns a string that represents the current object. (Inherited from Object) |
TrueForAll() | Determines whether every element in the array matches the conditions defined by the specified predicate. |
范例1:
// C# program to illustrate the Reverse() Method
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declares an 1D Array of string.
string[] topic;
// allocating memory for topic.
topic = new string[] {"Array, ", "String, ",
"Stack, ", "Queue, ",
"Exception, ", "Operators" };
// Displaying Elements of
// the array before reverse
Console.WriteLine("Topic of C# before reverse:");
Console.WriteLine();
foreach(string ele in topic)
{
Console.WriteLine(ele + " ");
}
Console.WriteLine();
// using Reverse() method to
// reverse the given array
Array.Reverse(topic);
// Displaying Elements of array after reverse
Console.WriteLine("Topic of C# after reverse:");
Console.WriteLine();
foreach(string val in topic)
{
Console.WriteLine(val + " ");
}
}
}
}
输出:
Topic of C# before reverse:
Array,
String,
Stack,
Queue,
Exception,
Operators
Topic of C# after reverse:
Operators
Exception,
Queue,
Stack,
String,
Array,
范例2:
// C# program to illustrate the Sort() Method
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declares an 1D Array of string.
string[] topic;
// allocating memory for topic.
topic = new string[] {"Array, ", "String, ",
"Stack, ", "Queue, ",
"Exception, ", "Operators" };
// Displaying Elements of the array before sort
Console.WriteLine("Topic of C# before reverse:");
Console.WriteLine();
foreach(string ele in topic)
{
Console.WriteLine(ele + " ");
}
Console.WriteLine();
// usign Sort() method to
// sort the given array
Array.Sort(topic);
// Displaying Elements of
// array after sort
Console.WriteLine("Topic of C# after reverse:");
Console.WriteLine();
foreach(string val in topic)
{
Console.WriteLine(val + " ");
}
}
}
}
输出:
Topic of C# before reverse:
Array,
String,
Stack,
Queue,
Exception,
Operators
Topic of C# after reverse:
Array,
Exception,
Operators
Queue,
Stack,
String,
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.array?view=netframework-4.7.2