当前位置: 移动技术网 > IT编程>脚本编程>Shell > Windows Powershell 复制数组

Windows Powershell 复制数组

2017年12月08日  | 移动技术网IT编程  | 我要评论

数组属于引用类型,使用默认的的赋值运算符在两个变量之间赋值只是复制了一个引用,两个变量共享同一份数据。这样的模式有一个弊病如果其中一个改变也会株连到另外一个。所以复制数组最好使用clone()方法,除非有特殊需求。

ps c:powershell> $chs=@("a","b","c")
ps c:powershell> $chsbak=$chs
ps c:powershell> $chsbak[1]="h"
ps c:powershell> $chs
a
h
c
ps c:powershell> $chs.equals($chsbak)
true
ps c:powershell> $chsnew=$chs.clone()
ps c:powershell> $chsnew[1]="good"
ps c:powershell> $chs.equals($chsnew)
false
ps c:powershell> $chs
a
h
c

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网