阵列 是大多数编程语言的基本语言特征。它们是一个值或物体的集合,因此难以避免。让我们仔细看看阵列和他们所提供的一切。

页面索引

什么是数组?

我将从什么是阵列的基本技术描述,并且在我转移到其他方式之前,大多数编程语言是如何使用的。

数组是一种数据结构,它用作多个项目的集合。您可以使用索引迭代数组或访问各个项目。该阵列被创建为顺序块的内存块,其中每个值在另一个旁边存储。

我们会在我们去的时候触摸这些细节。

基本用法

由于阵列是PowerShell的一种基本功能,因此在PowerShell中使用它们具有简单的语法。

使用@()创建数组

可以使用空数组 @()

    PS> $data = @()
    PS> $data.count
    0

We can create an array and seed it with values just by placing them in the @() parentheses.

    PS> $data = @('Zero','One','Two','Three')
    PS> $data.count
    4

    PS> $data
    Zero
    One
    Two
    Three

This array has 4 items. When we call the $data variable, we see the list of our items. If it is an array of strings, then we will have one line per string.

我们可以在多行上声明数组。在这种情况下,逗号是可选的,一般都遗漏了。

    $data = @(
        'Zero'
        'One'
        'Two'
        'Three'
    )

我更愿意在那样的多行上声明我的阵列。当您有多个项目时,不仅读取更容易读取,它还可以更轻松地与源控制时与以前的版本进行比较。

其他语法

It’s commonly understood that @() is the syntax for creating an array, but comma separated lists work most of the time.

    $data = 'Zero','One','Two','Three'

写入输出以创建数组

One cool little trick worth mentioning is that you can use Write-Output to quickly create strings at the console.

    $data = Write-Output Zero One Two Three

这是方便的,因为当参数接受字符串时,您不必在字符串周围引出引号。我永远不会在脚本中这样做,但它在控制台中是公平的游戏。

访问项目

既然您有一个带有物品的数组,您可能希望访问和更新这些项目。

抵消

To access individual items, we uses the brackets [] with an offset value starting at 0. This is how we get the first item in our array:

    PS> $data = 'Zero','One','Two','Three'
    PS> $data[0]
    Zero

我们在这里使用零的原因是因为第一个项目位于列表的开头,因此我们使用0项的偏移量来实现。要到达第二项,我们需要使用1的偏移量来跳过第一个项目。

    PS> $data[1]
    One

这意味着最后一个项目是偏移3。

    PS> $data[3]
    Three

指数

Now you can see why I picked the values that I did for this example. I introduced this as an offset because that is what it really is, but this offset is more commonly referred to as an index. An index that starts at 0. For the rest of this article I will call the offset an index.

特殊索引技巧

在大多数语言中,您只能将单个数字指定为索引,您将获得一个项目返回。 PowerShell更灵活。您可以立即使用多个索引。通过提供索引列表,我们可以选择多个项目。

    PS> $data[0,2,3]
    Zero
    Two
    Three

将根据提供的索引的顺序返回这些项目。如果复制索引,则会何时获得该项目。

    PS> $data[3,0,3]
    Three
    Zero
    Three

We can specify a sequence of numbers with the built in .. operator.

    PS> $data[1..3]
    One
    Two
    Three

这也反转了。

    PS> $data[3..1]
    Three
    Two
    One

You can use negitive index values to offset from the end. So if you need the last item in the list, you can use -1.

    PS> $data[-1]
    Three

One word of caution here with the .. operator. The sequence 0..-1 and -1..0 evaluate to the values 0,-1 and -1,0. It’s easy to see $data[0..-1] and think it would enumerate all items if you forget this detail. $data[0..-1] gives you the same value as $data[0,-1] by giving you the first and last item in the array (and none of the other values).

出界

在大多数语言中,如果您尝试访问超过阵列末尾的项目的索引,则会获得某些类型的错误或异常。 Powershell将默默地给你什么。

    PS> $null -eq $data[9000]
    True

无法索引到空数组中

If your variable is $null and you try to index it like an array, you will get a System.Management.Automation.RuntimeException exception with the message 无法索引到空数组中.

    PS> $empty = $null
    SP> $empty[0]
    Error: Cannot index into a null array.

So make sure your arrays are not $null before you try to access elements inside them.

数数

数组和其他集合具有计数属性,可以告诉您数组中有多少项。

    PS> $data.count
    4

电源外壳 3.0 added a count property to most objects. you can have a single object and it should give you a count of 1.

    PS> $date = Get-Date
    PS> $date.count
    1

Even $null has a count property except it returns 0.

    PS> $null.count
    0

There are some traps here that I will revisit when I cover checking for $null or empty arrays later on in this article.

通过一个错误

创建常见的编程错误,因为数组以索引开始0.关闭一个错误,可以以两个非常常见的方式引入。

The first is by mentally thinking you want the 2nd item and using an index of 2 and really getting the third item. Or by thinking that you have 4 items and you want last item, so you will just use the size to access the last item.

    $data[ $data.count ]

电源外壳 is perfectly happy to let you do that and give you exactly what item exists at index 4, $null. You should be using $data.count - 1 or the -1 那 we learned about above.

    PS> $data[ $data.count - 1 ]
    Three

This is where you can use the -1 index to get the last element.

    PS> $data[ -1 ]
    Three

Lee Dailey also pointed out to me that we can use $data.GetUpperBound(0) to get the max index number.

    PS> $data.GetUpperBound(0)
    Three

The second most common way is when iterating the list and just not stopping at the right time. I’ll revisit this when we talk about using the for loop.

更新项目

我们可以使用相同的索引来更新数组中的现有项目。这使我们可以直接访问更新单个项目。

    $data[2] = 'dos'
    $data[3] = 'tres'

If we try to update an item that is past the last element, then we get an 指数 was outside the bounds of the array. error.

    PS> $data[4] = 'four'
    指数 was outside the bounds of the array.
    At line:1 char:1
    + $data[4] = 'four'
    + ~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], 指数OutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException

当我谈论如何使数组更大时,我会稍后重新审视。

迭代

在某些时候,您需要散步或迭代整个列表并对数组中的每个项目执行一些操作。

管道

阵列和PowerShell管道彼此适合。这是处理这些值的最简单方法之一。将数组传递给管道时,数组内的每个项目都是单独处理的。

    PS> $data = 'Zero','One','Two','Three'
    PS> $data | ForEach-Object {"Item: [$PSItem]"}
    Item: [Zero]
    Item: [One]
    Item: [Two]
    Item: [Three]

If you have not seen $PSItem before, just know that its the same thing as $_. You can use either one because they both represent the current object in the pipeline.

foreach循环

The ForEach loop works well with collections. Using the syntax: foreach ( <variable> in <collection> )

    foreach ( $node in $data )
    {
        "Item: [$node]"
    }

Foreach方法

I tend to forget about this one but it works well for simple operations. PowerShell allows you to call .ForEach() on a collections.

    PS> $data.foreach({"Item [$PSItem]"})
    Item [Zero]
    Item [One]
    Item [Two]
    Item [Three]

The .foreach() takes a parameter that is a script block. You can drop the parentheses and just provide the script block.

    $data.foreach{"Item [$PSItem]"}

This is a lesser known syntax but it works just the same. This foreach method was added in PowerShell 4.0.

对于循环

The for loop is used heavily in most other languages but you don’t see it much in PowerShell. When you do see it, it is often in the context of walking an array.

    for ( $index = 0; $index -lt $data.count; $index++)
    {
        "Item: [{0}]" -f $data[$index]
    }

The first thing we do is initialize an $index to 0. Then we add the condition that $index must be less than $data.count. Finally, we specify that every time we loop that me must increase the index by 1. In this case $index++ is short for $index = $index + 1.

Whenever you are using a for loop, pay special attention to the condition. I used $index -lt $data.count here. It is very easy to get the condition slightly wrong to get an off by one error in your logic. Using $index -le $data.count or $index -lt ($data.count - 1) are ever so slightly wrong. That would cause your result to process too many or too few items. This is the classic off by one error.

切换循环

这是一个非常容易忽视的。如果您提供了一个数组 切换语句,它将匹配数组中的每个项目。

    $data = 'Zero','One','Two','Three'
    switch( $data )
    {
        'One'
        {
            'Tock'
        }
        'Three'
        {
            'Tock'
        }
        Default
        {
            'Tick'
        }
    }

这将产生此输出:

    Tick
    Tock
    Tick
    Tock

我们可以使用很多很酷的事情,我们可以使用Switch语句进行。我有另一篇专门的文章。

更新值

当您的阵列是字符串或整数的集合(值类型)时,有时您将想要在循环它们时更新数组中的值。上面的大多数循环在循环中使用变量来保存值的副本。如果更新该变量,则不会更新数组中的原始值。

The exception to that statement is the for loop. If you are wanting to walk an array and update values inside it, then the for loop is what you are looking for.

    for ( $index = 0; $index -lt $data.count; $index++ )
    {
        $data[$index] = "Item: [{0}]" -f $data[$index]
    }

此示例按索引取值,进行了一些更改,然后使用同一索引来分配回来。

对象数组

到目前为止,我们唯一放置在数组中的唯一是值类型,但阵列也可以包含对象。

   $data = @(
       [pscustomobject]@{FirstName='Kevin';LastName='Marquette'}
       [pscustomobject]@{FirstName='John'; LastName='Doe'}
   )

将它们分配给变量时,许多cmdlets将对象的集合作为数组返回为数组。

    $processList = Get-Process

我们已经谈过的所有基本功能仍然适用于物体阵列,其中一些细节值得指出。

访问属性

我们可以使用索引来访问集合中的单个项目,就像有价值类型一样。

    PS> $data[0]

    FirstName LastName
    -----     ----
    Kevin     Marquette

我们可以直接访问和更新属性。

    PS> $data[0].FirstName

    Kevin

    PS> $data[0].FirstName = 'Jay'
    PS> $data[0]

    FirstName LastName
    -----     ----
    Jay       Marquette

数组属性

通常,您必须枚举像这样的整个列表以访问所有属性:

    PS> $data | ForEach-Object {$_.LastName}

    Marquette
    Doe

Or by using the Select-Object -ExpandProperty cmdlet.

    PS> $data | Select-Object -ExpandProperty LastName

    Marquette
    Doe

But PowerShell offers us the ability to request LastName directly. PowerShell will enumerate them all for us and give us a clean list.

    PS> $data.LastName

    Marquette
    Doe

枚举仍然发生,但我们没有看到它背后的复杂性。

其中 - 对象过滤

This is where Where-Object comes in so we can filter and select what we want out of the array based on the properties of the object.

    PS> $data | Where-Object {$_.FirstName -eq 'Kevin'}

    FirstName LastName
    -----     ----
    Kevin     Marquette

We can write that same query like this to get the FirstName we are looking for.

    $data | Where FirstName -eq Kevin

在哪里()

阵列 have a 在哪里() method on them that allows you to specify a scriptblock for the filter.

    $data.Where({$_.FirstName -eq 'Kevin'})

此功能已在PowerShell 4.0中添加。

更新循环中的对象

具有值类型,更新数组的唯一方法是使用A for循环,因为我们需要知道索引来替换该值。我们有更多的选项,因为它们是引用类型。这是一个快速示例:

    foreach($person in $data)
    {
        $person.FirstName = 'Kevin'
    }

This loop is walking every object in the $data array. Because objects are reference types, the $person variable references the exact same object that is in the array. So updates to it’s properties will update the original.

You still can’t replace the whole object this way. If you try to assign a new object to the $person variable, you are updating the variable reference to something else that no longer points to the original object in the array. This will not work like you would expect:

    foreach($person in $data)
    {
        $person = [pscustomobject]@{
            FirstName='Kevin'
            LastName='Marquette'
        }
    }

运营商

电源外壳中的运营商也在阵列上工作。其中一些人略有不同。

-加入

The -加入 operator is the most obvious one so we will look at it first. I like the -加入 operator and use it often. It will join all elements in the array with a character or string that you specify.

    PS> $data = @(1,2,3,4)
    PS> $data -加入 '-'
    1-2-3-4
    PS> $data -加入 ','
    1,2,3,4

One of the features that I like about the -加入 operator is that it handles single items.

    PS> 1 -加入 '-'
    1

我在日志记录和冗长的消息中使用了这个。

    PS> $data = @(1,2,3,4)
    PS> "Data is $($data -加入 ',')."
    Data is 1,2,3,4.

-加入 $ array.

这是一个聪明的伎俩,即李德利指出我。如果您想在没有分隔符的情况下加入所有内容,而不是这样做:

    PS> $data = @(1,2,3,4)
    PS> $data -加入 $null
    1234

You can use -加入 with the array as the parameter with no prefix. Take a look at this example to see that I am talking about.

    PS> $data = @(1,2,3,4)
    PS> -加入 $data
    1234

-replace和-split.

The other operators like -replace and -split will execute on each item in the array. I can’t say that I have ever used them this way but here is an example.

    PS> $data = @('ATX-SQL-01','ATX-SQL-02','ATX-SQL-03')
    PS> $data -replace 'ATX','LAX'
    LAX-SQL-01
    LAX-SQL-02
    LAX-SQL-03

- 索引

The - 索引 operator will allow you to check an array of values to see if it contains a specified value.

    PS> $data = @('red','green','blue')
    PS> $data  - 索引 'green'
    True

-在

When you have a single value that you would like to verify matches one of several values, you can use the -在 operator. The value would be on the left and the array on the right hand side of the operation.

    PS> $data = @('red','green','blue')
    PS> 'green' -在  $data
    True

如果列表很大,这会变得昂贵。如果我检查超过4-5个值,我会经常使用正则表达式模式。

    PS> $data = @('red','green','blue')
    PS> $pattern = "^({0})$" -f ($data -加入 '|')
    PS> $pattern
    ^(red|green|blue)$

    PS> 'green' -比赛 $pattern
    True

-eq和-ne.

Equality and arrays can get complicated. When the array is on the left side, every item will get compared. Instead of returning True, it will return the object that matches.

    PS> $data = @('red','green','blue')
    PS> $data -eq 'green'
    green

When you use the -ne operator, we will get all the values that are not equal to our value.

    PS> $data = @('red','green','blue')
    PS> $data -ne 'green'
    red
    blue

When you use this in an if() statement, a value that is returned is a True value. If no value is returned then it is a False value. Both of these next statements will evaluate to True.

    $data = @('red','green','blue')
    if ( $data -eq 'green' )
    {
        'Green was found'
    }
    if ( $data -ne 'green' )
    {
        'And green was not found'
    }

I’ll revisit this in a moment when we talk about testing for $null.

-比赛

The -比赛 operator will try to match each item in the collection.

    PS> $servers = @(
          'LAX-SQL-01'
          'LAX-API-01'
          'ATX-SQL-01'
          'ATX-API-01'
        )
    PS> $servers -比赛 'SQL'
    LAX-SQL-01
    ATX-SQL-01

When you use -比赛 with a single value, a special variable $Matches gets populated with match info. This is not the case when an array is processed this way.

We can take the same approach with Select-String.

    $servers | Select-String SQL

I take a closer look at Select-String,-比赛 and the $matches variable in another post called 使用正则表达式的许多方法.

$ null或空

Testing for $null or empty arrays can be tricky. Here are the common traps with arrays.

这句话一目了然,看起来应该有效。

    if ( $array -eq $null)
    {
        'Array is $null'
    }

But I just went over how -eq checks each item in the array. So we can have an array of several items with a single $null value and it would evaluate to $true

    $array = @('one',$null,'three')
    if ( $array -eq $null)
    {
        'I think Array is $null, but I would be wrong'
    }

This is why it’s a best practice to place the $null on the left side of the operator. This makes this scenario a non-issue.

    if ( $null -eq $array )
    {
        'Array actually is $null'
    }

A $null array is not the same thing as an empty array. If you know you have an array, check the count of objects in it. If the array is $null, the count will be 0.

    if ( $array.count -gt 0 )
    {
        'Array is not empty'
    }

There is one more trap to watch out for here. You can use the count even if you have a single object, unless that object is a PSCustomObject. This is a bug that is fixed in PowerShell 6.1. That’s good news, but a lot of people are still on 5.1 and need to watch out for it.

    PS> $object = [PSCustomObject]@{Name='TestObject'}
    PS> $object.count
    $null

如果您仍然在PowerShell 5.1上,您可以在检查计数之前在数组中包装对象以获得准确的计数。

    if ( @($array).count -gt 0 )
    {
        'Array is not empty'
    }

To fully play it safe, check for $null, then check the count.

    if ( $null -ne $array -and @($array).count -gt 0 )
    {
        'Array is not empty'
    }

所有-eq.

我最近看到有人问 如何验证数组中的每个值是否匹配给定值。 reddit用户/ u / bis有这个聪明 解决方案 这检查了任何错误的值,然后翻转结果。

    $results = Test-Something
    if ( -not ( $results -ne 'Passed') )
    {
        'All results a Passed'
    }

添加到阵列

此时,您开始怀疑如何将项目添加到数组。快速答案是你不能。数组是内存中的固定大小。如果您需要长大或添加单个项目,则需要创建一个新数组并复制旧数组上的所有值。这听起来很昂贵,但很多工作,但是,PowerShell隐藏了创建新阵列的复杂性。

数组添加

我们可以使用带有数组的加法运算符来创建新的数组。所以给了这两个数组:

    $first = @(
        'Zero'
        'One'
    )
    $second = @(
        'Two'
        'Three'
    )

我们可以将它们添加在一起以获得一个新的阵列。

    PS> $first + $second

    Zero
    One
    Two
    Three

加上等于+ =

我们可以创建一个新的阵列,并将项目添加到它中:

    $data = @(
        'Zero'
        'One'
        'Two'
        'Three'
    )
    $data += 'four'

Just remember that every time you use += 那 you are duplicating and creating a new array. This is a not an issue for small datasets but it scales extremely poorly.

管道分配

您可以将任何管道的结果分配到变量中。如果它包含多个项目,它将是一个数组。

    $array = 1..5 | ForEach-Object {
        "ATX-SQL-$PSItem"
    }

Normally when we think of using the pipeline, we think of the typical PowerShell one-liners. We can leverage the pipeline with foreach() statements and other loops. So instead of adding items to an array in a loop, we can drop items onto the pipeline.

    $array = foreach ( $node in (1..5))
    {
        "ATX-SQL-$node"
    }

By assigning the results of the foreach to a variable, we capture all the objects and create a single array.

数组类型

By default, an array in PowerShell is created as a [PSObject[]] type. This allows it to contain any type of object or value. This works because everything is inherited from the PSObject type.

强类型阵列

您可以使用类似的语法创建任何类型的数组。创建强类型的数组时,它只能包含指定类型的值或对象。

    PS> [int[]] $numbers = 1,2,3
    PS> [int[]] $numbers2 = 'one','two','three'
    ERROR: Cannot convert value "one" to type "System.Int32". Input string was not in a correct format."

    PS> [string[]] $strings = 'one','two','three'

数组列表

向数组添加项目是其最大限制之一,但有一些其他集合可以转向解决此问题。

The 数组列表 is commonly one of the first things that we think of when we need an array that is faster to work with. It acts like an object array every place that we need it, but it handles adding items quickly.

Here is how we create an 数组列表 and add items to it.

    $myarray = [System.Collections.数组列表]::new()
    [void]$myArray.Add('Value')

We are calling into .Net to get this type. In this case we are using the default constructor to create it. Then we call the Add method to add an item to it.

The reason I am using [void] at the beginning of the line is to suppress the return code. Some .Net calls will do this and can create unexpected output.

如果您在数组中唯一的数据是字符串,那么也看一下使用 StringBuilder.. It is almost the same thing but has some methods that are just for dealing with strings. The StringBuilder. is specially designed for performance.

It is really common to see people move to 数组列表 from arrays. But it comes from a time where C# did not have generic support. The 数组列表 is depreciated in support for the generic List[]

通用列表

A generic type is a special type in C# that define a generalized class and the user will specify the data types it will use when created. So if you want a list of numbers or strings, you would define that you want list of int or string types.

以下是您为字符串创建列表的方式。

    $mylist = [System.Collections.Generic.List[string]]::new()

或数字列表。

    $mylist = [System.Collections.Generic.List[int]]::new()

我们可以将现有数组投入到这样的列表中,而无需创建对象:

    $mylist = [System.Collections.Generic.List[int]]@(1,2,3)

We can shorten the syntax a little bit with the using namespace statement in PowerShell 5 and newer. The using statement needs to be the first line of your script. By declaring a namespace, PowerShell will let you leave it off of the datatypes when you reference them.

    using namespace System.Collections.Generic
    $myList = [List[int]]@(1,2,3)

This makes the List much more usable.

You have a similar Add method available to you. Unlike the ArrayList, there is no return value on the Add method so we don’t have to void it.

    $myList.Add(10)

我们仍然可以像其他数组一样访问元素。

    PS> $myList[-1]
    10

列表[psobject]

You can have a list of any type, but when you don’t know the type of objects, you can use [List[PSObject]] to contain them.

    $list = [List[PSObject]]::new()

去掉()

The 数组列表 and the generic List[] both support removing items from the collection.

    using namespace System.Collections.Generic
    $myList = [List[string]]@('Zero','One','Two','Three')
    [void]$myList.Remove("Two")
    Zero
    One
    Three

使用值类型时,它将从列表中删除第一个。您可以在又一遍地调用它以继续删除该价值。如果您有引用类型,则必须提供要删除的对象。

    [list[System.Management.Automation.PSDriveInfo]]$drives = Get-PSDrive
    $drives.remove($drives[2])
    $delete = $drives[2]
    $drives.remove($delete)

如果能够从集合中查找和删除该项目,则删除方法将返回true。

更多收藏品

还有许多其他集合可以使用,但这些是替代良好的通用数组。如果您有兴趣了解更多这些选项,请看看这一点 要旨马克克劳斯 put together.

其他细微差别

现在我已经涵盖了所有的主要功能,这里有几件我想提到的事情在我包装之前。

预先大小的阵列

I mentioned that you can’t change the size of an array once it is created. We can create an array of a pre-determined size by calling it with the new($size) constructor.

    $data = [Object[]]::new(4)
    $data.count
    4

乘以阵列

一个有趣的小技巧是您可以将数组乘以整数。

    PS> $data = @('red','green','blue')
    PS> $data * 3
    red
    green
    blue
    red
    green
    blue
    red
    green
    blue

用0初始化

常见的方案是您希望使用所有零创建一个数组。如果您只有整数,则默认为所有零的强类型的整数数组。

    PS> [int[]]::new(4)
    0
    0
    0
    0

我们也可以使用乘法技巧来执行此操作。

    PS> $data = @(0) * 4
    PS> $data
    0
    0
    0
    0

The nice thing about the multiplying trick is that you can use any value. So if you would rather have 255 as your default value, this would be a good way to do it.

    PS> $data = @(255) * 4
    PS> $data
    255
    255
    255
    255

嵌套阵列

数组内的数组称为嵌套数组。我不在PowerShell中使用这些,但我已经更多地使用了其他语言。当数据适合像格式的网格时,请考虑使用数组数组。

以下是我们可以创建二维数组的两种方式。

    $data = @(@(1,2,3),@(4,5,6),@(7,8,9))

    $data2 = @(
        @(1,2,3),
        @(4,5,6),
        @(7,8,9)
    )

逗号在这些例子中非常重要。我在逗号是可选的多行上的普通数组的早期示例。这不是多维阵列的情况。

The way we use the index notation changes slightly now that we have a nested array. Using the $data above, this is how we would access the value 3.

    PS> $outside = 0
    PS> $inside = 2
    PS> $data[$outside][$inside]
    3

为每个级别的阵列嵌套添加一组括号。第一组括号是用于最外层的阵列,然后你从那里工作。

写输出 - NoEnumerate

powershell喜欢打开或枚举数组。这是PowerShell使用管道的方式的核心方面,但有时有时不希望发生这种情况。

I commonly pipe objects to Get-Member to learn more about them. When I pipe an array to it, it gets unwrapped and Get-Member sees the members of the array and not the actual array.

    PS> $data = @('red','green','blue')
    PS> $data | Get-Member
    TypeName: System.String
    ...

To prevent that unwrap of the array, you can use Write-Object -NoEnumerate.

    PS> Write-Output -NoEnumerate $data | Get-Member
    TypeName: System.Object[]
    ...

我有一个第二种方法来做这更像是一个黑客(我试图避免像这样的黑客)。您可以在将阵列前面放置逗号。

    PS> ,$data | Get-Member
    TypeName: System.Object[]
    ...

返回阵列

当您从函数输出或返回值时,也会发生此阵列的未包装。如果您将输出分配给变量,则仍然可以获得数组,因此这不是一个问题。

The catch is that you will have a new array. If that is ever a problem, you can use 写输出 - NoEnumerate $array or return ,$array to work around it.

还要别的吗?

我知道这一切都是很多东西。我的希望是每次阅读它,你都会从这篇文章中学到一些东西,并且它将成为很长一段时间的很好的参考。如果您发现这是有帮助的,请与其他人分享,您认为将获得估值。

从这里,我建议你看看我写的类似帖子 Hashtables..