为什么使用 ArrayList 会打印数字? [英] Why does using an ArrayList prints numbers?

查看:25
本文介绍了为什么使用 ArrayList 会打印数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入数据:

<前>(00:00) Pedro Del Mar & Beatsole - Pianophoria (Original Mix)https://www.beatport.com/track/pianophoria-original-mix/5970941【黑洞录音】03:01 新世界 - 潮 (Original Mix)https://www.beatport.com/track/ushio-original-mix/6021871[阿波拉录音]

PowerShell 代码:

$readData = Get-Content "C:\Users\SomeUser\inputData - Alternate - 2.txt";$ArrayList = 新对象 System.Collections.ArrayList函数 removeBrackets([string]$removeBrackets){if($removeBrackets.Contains(":") -and ($removeBrackets.IndexOf(":") -eq 2)){$subString = $removeBrackets.Substring(7-1);$ArrayList.Add($subString)}elseif($removeBrackets.Contains(":") -and ($removeBrackets.IndexOf(":") -eq 3)){$subString = $removeBrackets.Substring(7+1);$ArrayList.Add($subString)}}for($i=0; $i -le $readData.Length; $i++){removeBrackets($readData[$i])}for($a=0; $a -le $ArrayList.Count; $a++){写输出 $ArrayList[$a]}

输出:

<前>01Pedro Del Mar & Beatsole - Pianophoria (Original Mix)新世界 - 潮 (Original Mix)

问题:

我创建了一个脚本,用于过滤文本文件中的数据.它从轨道中删除 HTTP 链接和时间.然后将字符串添加到数组列表

为什么打印数组列表会产生0,1?我如何防止这种情况发生?

解决方案

ArrayList.Add 方法总是返回您添加的新项目的索引:

PS >$ArrayList = 新对象 System.Collections.ArrayListPS>$ArrayList.Add('a')0PS>$ArrayList.Add('b')1PS>

您可以通过强制转换为 [void] 来抑制此输出:

PS >$ArrayList = 新对象 System.Collections.ArrayListPS>[void]$ArrayList.Add('a')PS>

或者通过将输出重定向到 $null:

PS >$ArrayList = 新对象 System.Collections.ArrayListPS>$ArrayList.Add('a') >$nullPS>

Input Data:

(00:00) Pedro Del Mar & Beatsole - Pianophoria (Original Mix)
https://www.beatport.com/track/pianophoria-original-mix/5970941
[Black Hole Recordings]

03:01 New World - Ushio (Original Mix) 
https://www.beatport.com/track/ushio-original-mix/6021871
[Abora Recordings]

PowerShell Code:

$readData = Get-Content "C:\Users\SomeUser\inputData - Alternate - 2.txt"
$ArrayList = New-Object System.Collections.ArrayList


function removeBrackets([string]$removeBrackets){


if($removeBrackets.Contains(":") -and ($removeBrackets.IndexOf(":")  -eq 2)){
           $subString = $removeBrackets.Substring(7-1);
           $ArrayList.Add($subString)
          
   
  }
elseif($removeBrackets.Contains(":") -and ($removeBrackets.IndexOf(":")  -eq 3)){ 
         $subString = $removeBrackets.Substring(7+1);
         $ArrayList.Add($subString)
      
     }

}

for($i=0; $i -le $readData.Length; $i++){

     
     removeBrackets($readData[$i])


}

for($a=0; $a -le $ArrayList.Count; $a++){

   Write-Output $ArrayList[$a]
}

Output:

0
1
Pedro Del Mar & Beatsole - Pianophoria (Original Mix)
New World - Ushio (Original Mix)

Problem:

I created a script that filters out data from a text file. It removes the HTTP links and times from the tracks. It then adds the string to an Array List

Why does printing the Array List produce the 0,1? How do I prevent that from happening?

解决方案

The ArrayList.Add method always returns the index of the new item that you add:

PS > $ArrayList = New-Object System.Collections.ArrayList
PS > $ArrayList.Add('a')
0
PS > $ArrayList.Add('b')
1
PS >

You can suppress this output by either casting to [void]:

PS > $ArrayList = New-Object System.Collections.ArrayList
PS > [void]$ArrayList.Add('a')
PS >

or by redirecting the output to $null:

PS > $ArrayList = New-Object System.Collections.ArrayList
PS > $ArrayList.Add('a') > $null
PS > 

这篇关于为什么使用 ArrayList 会打印数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆