【问题标题】:How can you add values into an arraylist如何将值添加到数组列表中
【发布时间】:2020-05-15 11:39:11
【问题描述】:

我有两组变量

$numbers = "1","2","3"
$records = "A","B","C"

我想一起加入,所以最终输出是

 Name                           Value                                  
 ----                           -----                                                                                                                                      
 A                              {1}                                     
 B                              {2}                                     
 C                              {3}

到目前为止,我已经成功构建了 Arraylist

$numbers = "1","2","3"
$records = "A","B","C"

$total = [ordered] @{}
Foreach ($entry in $records){ $total[$entry] = New-Object Collections.Arraylist }

这给了我

 Name                           Value                                  
 ----                           -----                                                                                                                                      
 A                              {}                                     
 B                              {}                                     
 C                              {}

但是,当我尝试添加数字时,我得到了这个结果,但我不知道我做错了什么。请有人可以将我推向正确的方向。谢谢。

ForEach ($row in $numbers)

    { $total[$records] += $row}

  Name                           Value                                  
 ----                           -----                                                                                                                                      
 A                              {}                                     
 B                              {}                                     
 C                              {}
 {A, B, C}                      {, , , 3} 

【问题讨论】:

    标签: powershell


    【解决方案1】:

    美好的开始。您当前的方法只需要考虑何时遇到新密钥或现有密钥。

    您可以采取的主要方法是迭代记录,并首先检查记录是否作为哈希表中的键存在。如果是,则创建一个新的System.Collections.ArrayList,否则将该数字添加到现有密钥的ArrayList。这假设两个数组的长度相等。

    这是一种方法:

    $numbers = "1","2","3"
    $records = "A","B","C"
    
    $total = [ordered] @{}
    for ($i = 0; $i -lt $records.Length; $i++) {
        $record = $records[$i]
    
        # Check if record key exists here
        if ($total.Keys -notcontains $record) {
    
            # Create new array list for the new record key found
            $total[$record] = New-Object -TypeName System.Collections.Arraylist
        }
    
        # Add item to array list in hashtable
        [void]$total[$record].Add($numbers[$i])
    }
    
    $total
    

    为了检查密钥是否存在,我使用-notcontains 检查哈希表Keys 中是否存在密钥。您可以查看about_comparison_operators,了解有关 PowerShell 中可用运算符的更多信息。

    此外,要将对象添加到ArrayList,您可以使用System.Collections.ArrayList.Add(Object)。此方法返回类型int,即添加值的索引。这意味着每次添加对象时,您都会将索引输出到屏幕。为了抑制这种情况,我投了void,它忽略了返回值。如果您想知道其他方法,请查看Prevent ArrayList.Add() from returning the index 问题。

    输出:

    Name                           Value
    ----                           -----
    A                              {1}
    B                              {2}
    C                              {3}
    

    【讨论】:

    • 谢谢,这非常有效,对我的理解有很大帮助。
    • @Jamie_lee 不用担心,很高兴我能帮上忙。
    【解决方案2】:

    如果大括号不重要,因为您显示的值只是一个哈希:

    $numbers = "1","2","3"
    $records = "A","B","C"
    $hash    = [ordered]@{}
    
    for( $i = 0; $i -lt $numbers.Length; $i++ ) {
        for( $j = 0; $j -lt $records.Length; $j++ ) {
            if( $i -eq $j ) {
                $hash[ "$( $records[ $j ])" ] = "$( $numbers[ $i ] )"
                continue
            }
        }
    }
    
    $hash
    

    输出:

    Name                           Value
    ----                           -----
    A                              1
    B                              2
    C                              3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      相关资源
      最近更新 更多