当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例

作者:精品下载站 日期:2024-12-14 05:00:59 浏览:13 分类:玩电脑

PowerShell 列表 | PowerShell列表的方法和示例


[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例

PowerShell列表简介

PowerShell 列表是一种用于存储项目列表的数据类型或数据结构。列表的大小不是固定的,可以随时更改。列表相对于数组的主要优点是列表可以保存不同数据类型的项,因此它被认为是强数据类型。这存在于系统中。集合点网类。有许多方法和对象与列表关联。本文将详细介绍该列表及其实现以及适当的示例。

语法:

在 PowerShell 中创建列表有两种方法

$testlist = New-Object -TypeName 'System.Collections.ArrayList';

或者

$testlist = [System.Collections.ArrayList]::new()

空列表可以定义为

[System.Collections.ArrayList]$testlist= @()

例如。:

输入:

Write-Host "Demo of list in Powershell"
$testlist = New-Object -TypeName 'System.Collections.ArrayList'
Write-Host "Datatype is " $testlist.GetType() -ForegroundColor Green
$testlist1 = [System.Collections.ArrayList]::new()
Write-Host "Datatype is " $testlist1.GetType() -ForegroundColor Green
Write-Host "Declaring an empty list"
[System.Collections.ArrayList]$testlist2= @()
Write-Host "Datatype is " $testlist2.GetType() -ForegroundColor Green

输出:

Demo of list in Powershell
Datatype is  System.Collections.ArrayList
Datatype is  System.Collections.ArrayList
Declaring an empty list
Datatype is  System.Collections.ArrayList

可用于 ArrayList 的方法

以下是一些可用于列表的方法。它们是 add、addrange、binarysearch、clear、clone、contains、copyto、equals、getenumerator、gethashcode、getrange、gettype、insertrange、setrange、removerange、remove、removeat、sort、toarray、tostring、where、trimtosize、item 和 foreach。

以下是属性:capacity、count、isfixedsize、isreadonly、issynchronized、syncroot。

方法及使用

Add():该方法用于向列表中添加一个元素

Clear():该方法用于清空列表内容

Contains():此方法用于检查列表中是否存在某个项目

Sort():该方法用于对列表中项目的元素进行排序

Remove():此方法删除指定值的元素

RemoveAt(): 此方法删除指定位置处的元素。

Clone():此方法创建列表的副本

CopyTo():该方法用于将一个列表的内容复制到另一个列表

InsertRange():该方法向列表中指定范围内插入一个新元素

TrimtoSize(): 该元素将列表缩小到指定大小

Foreach():该方法用于循环

GetType():该方法用于查找列表的类型

RemoveRange():此方法删除指定范围内的项目列表

ToArray():此方法将列表转换为数组

PowerShell 列表示例

下面给出了 PowerShell 列表的示例:

例子#1

将项目添加到列表

输入:

Write-Host "Demo of adding items to a list" -ForegroundColor Green
$dlist = New-Object -TypeName 'System.Collections.ArrayList';
Write-Host "The data type is" $dlist.GetType() -ForegroundColor Green
$dlist.Add("vignesh")
$dlist.Add("nandhini")
$dlist.Add(100)
$dlist.Add("100sfddf")
$dlist.Add("1&*(")
$dlist.Add(909)
Write-Host "the items in the list are" -ForegroundColor Green $dlist

输出:

[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例

例子#2

从列表中删除项目

输入:

Write-Host "Demo of removing items from a list" -ForegroundColor Green
$dlist = New-Object -TypeName 'System.Collections.ArrayList';
Write-Host "The data type is" $dlist.GetType() -ForegroundColor Green
$dlist.Add("vignesh")
$dlist.Add("nandhini")
$dlist.Add(100)
$dlist.Add("100sfddf")
$dlist.Add("1&*(")
$dlist.Add(909)
$dlist.Add("Vyapini")
$dlist.Add("Vijay")
$dlist.Add("VJS")
$dlist.Add("VVA")
Write-Host "the items in the list are" -ForegroundColor Green $dlist
Write-Host "The count of items in the list is " $dlist.Count
$cin= Read-Host "Enter the position of the element to be deleted"
$dlist.RemoveAt($cin)
Write-Host "Now the items in the list are" $dlist -ForegroundColor Green
$cin= Read-Host "Enter the element to be deleted"
$dlist.Remove($cin)
Write-Host "Now the elements in the list are" -ForegroundColor Green $dlist
Write-Host "Demo of deleting multiple items"
$index= Read-Host "enter the index from where it has to be deleted"
$range= Read-Host "Enter the number of items to be deleted"
$dlist.RemoveRange($index,$range)
Write-Host "Now the elements in the list are" -ForegroundColor Green $dlist

输出:

[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例

例子#3

对列表中的项目进行排序和循环

输入:

Write-Host "Demo of sorting items in a list" -ForegroundColor Green
$dlist = New-Object -TypeName 'System.Collections.ArrayList';
Write-Host "The data type is" $dlist.GetType() -ForegroundColor Green
$dlist.Add("vignesh")
$dlist.Add("nandhini")
$dlist.Add(100)
$dlist.Add("100sfddf")
$dlist.Add("1&*(")
$dlist.Add(909)
$dlist.Add("Vyapini")
$dlist.Add("Vijay")
$dlist.Add("VJS")
$dlist.Add("VVA")
Write-Host "the items in the list are" -ForegroundColor Green $dlist
Write-Host "Sorting the items in the list in ascending order" -ForegroundColor Green
$dlist |Sort-Object
Write-Host "sorting the items in the list in desceding order" -ForegroundColor Green
$dlist |Sort-Object Descending
Write-Host "looping the list items" -ForegroundColor Green
foreach($d in $dlist)
{
Write-Host "The current element is" $d
}

输出:

[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例

[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例

例子#4

输入:

Write-Host "Demo of various methods in a list" -ForegroundColor Green
$dlist = New-Object -TypeName 'System.Collections.ArrayList';
Write-Host "The data type is" $dlist.GetType() -ForegroundColor Green
$dlist.Add("vignesh")
$dlist.Add("nandhini")
$dlist.Add(100)
$dlist.Add("100sfddf")
$dlist.Add("1&*(")
$dlist.Add(909)
$dlist.Add("Vyapini")
$dlist.Add("Vijay")
$dlist.Add("VJS")
$dlist.Add("VVA")
Write-Host "count of the list is:" -ForegroundColor Green $dlist.Count
Write-Host "capacity of the list is:" -ForegroundColor Green $dlist.Capacity
Write-Host "is the list synchronized:" -ForegroundColor Green $dlist.IsSynchronized
Write-Host "is the list of fixed size" -ForegroundColor Green $dlist.IsFixedSize
write-Host "Is the list read-only" -ForegroundColor Green $dlist.IsReadOnly
write-Host "Is the list synced" -ForegroundColor Green $dlist.SyncRoot
$dlist.AddRange(@("a","b"))
Write-Host "The elements in the list now are" $dlist -ForegroundColor Green
Write-Host "Cloning the list" -ForegroundColor Green
$dlistclone=$dlist.Clone()
Write-Host "Elements in clone are" $dlistclone -ForegroundColor Green
Write-Host "clearing the contents of the list" -ForegroundColor Green
$dlistclone.Clear()
Write-Host "size of the clone list" $dlistclone.Count -ForegroundColor Green
Write-Host "check if the item is there in the list" -ForegroundColor Green
$toc= Read-Host "enter the item to be checked" -ForegroundColor Green
$dlist.Contains($toc)
$dlist1 = New-Object -TypeName 'System.Collections.ArrayList';
$dlist1.Add("100000")
$dlist1.Add("999999")
Write-Host "copying list items from one list to another" -ForegroundColor Green
$dlist1.CopyTo($dlist)
Write-Host "new values in dlist1:" $dlist -ForegroundColor Green
Write-Host "checking two lists" -ForegroundColor Green
$dlist.Equals($dlist1)
Write-Host "getting range" -ForegroundColor Green
$dlist.GetRange(1,2)
Write-Host "inserting a value at the specified position" -ForegroundColor Green
$ind= Read-Host "enter the index at which the item has to be inserted"
$val= Read-Host "enter the value"
$dlist.Insert($ind,$val)
Write-Host "now the values of the list " $dlist -ForegroundColor Green
Write-Host "reducing the size of the list" -ForegroundColor Green
$siz= Read-Host "enter the size of the list to be"
$dlist.TrimToSize()
Write-Host "current length of the list is" -ForegroundColor Green $dlist.Count

输出:

[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例

[玩转系统] PowerShell 列表 | PowerShell列表的方法和示例

结论

因此,本文详细介绍了 PowerShell 中的列表。它详细解释了与列表相关的各种属性和方法、其用法和类型,以及适当的示例。要了解有关 PowerShell 列表的更多信息,建议编写示例脚本并进行练习。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯