博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Powershell] Powershell dynamic arrays
阅读量:5321 次
发布时间:2019-06-14

本文共 4421 字,大约阅读时间需要 14 分钟。

Or we can say C#? Since powershell is from C#. For sure the subject is aganist itself, according to belong page of MSDN, the IsFixedSize property is always true for all arrays.

Welcome email to: larry.song@outlook.com

https://msdn.microsoft.com/en-us/library/system.array.isfixedsize(v=vs.110).aspx

That means the number of elements in a single array is fixed, we can use below codes to add new elements but it actually cloned a new array and resized it, the address pointing is broken.

$t = 1..2$r = $t$r[1] = 9$t # output of $t is 1,9 because $r and $t pointing to the same array like pointing in C######$r += 10$t # output of $t keep 1,9 this is because a new array cloned and resized with new elements

Ways like this is quite simply for everyone, but of course costs resources of system, script is a script, we need it easy for using otherwise it doesn't be called as script.

Like the codes above, the way was simply enough but broke the fact $r and $t original pointing to a same array, which i can update element values in one variable, another will follow. but the way adding new elements cloned a new array and different pointings.

Sometimes I really need 2 or more variables pointing to a same array update values together, communication between threads as example, at the same time I also have to add/remove elements.

People like me might be thinking there are methods from array itself to do such requirement. the fact is true and false at the same time. see below,

Remove         Method                void IList.Remove(System.Object value)RemoveAt       Method                void IList.RemoveAt(int index) # when using remove and removeat methods, errors will be thrown says the collection is size fixed.Clear             Method     static void Clear(array array, int index, int length) # when using [array]::clear($t, 1, 1), yes it can clear the vaule but the total number of elements will not be updated.

All I want is multiple varibles pointing to the same array and I can update elements number without impacting the first rule.

I keep my focus on array object, thought there might be wonderful ways to achieve I want, fact prove I am wrong. So I turned my face to the property IsFixedSize, I want to find another object like array but the property to be false, this is where ArrayList comes from.

https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx

$t = New-Object System.Collections.ArrayList$t.IsFixedSize# output is false

Good good, the first step achieved, it dynamic and usage like a normal array. it feeds new elements like hash table which is so friendly.

Add            Method                int Add(System.Object value), int IList.Add(System.Object v...AddRange       Method                void AddRange(System.Collections.ICollection c)Remove         Method                void Remove(System.Object obj), void IList.Remove(System.Ob...RemoveAt       Method                void RemoveAt(int index), void IList.RemoveAt(int index)RemoveRange    Method                void RemoveRange(int index, int count)TrimToSize     Method                void TrimToSize()Capacity       Property              int Capacity {get;set;}Count          Property              int Count {get;}

Add is to add new single element at the end, AddRange copy another collection object to its end, elements adding/removing problem solved.

The 3 ways regarding to Remove are to remove elements which solve problems on removing.

Count and Capacity property will increase themselves with elements adding, but capacity will not follow elements removing, which count does, this can help on getting element.

TrimToSize is to align count and capacity, reset capacity to its count.

Until now, what I want "multiple varibles pointing to the same array and I can update elements number without impacting the first rule" is achieved.

Yes, this way seems much easier and wonderful, how about cost?

No matter memory or CPU it takes, it much than array, one simplies testing is open 2 powershell.exe and use array and arraylist to build 2 new big array like 1..1000000, we can see the diffenerce from taskmgr, CPU time is too shore to notice by human which surely cost more. The easy and friendly are based on background C# codes, although we don't know it but it really exists anyway, as I said, script is a script, automation is first priority.

转载于:https://www.cnblogs.com/LarryAtCNBlog/p/4252464.html

你可能感兴趣的文章
2018.11.06 bzoj1040: [ZJOI2008]骑士(树形dp)
查看>>
2019.02.15 bzoj5210: 最大连通子块和(链分治+ddp)
查看>>
redis cluster 集群资料
查看>>
微软职位内部推荐-Sr. SE - Office incubation
查看>>
微软职位内部推荐-SOFTWARE ENGINEER II
查看>>
centos系统python2.7更新到3.5
查看>>
C#类与结构体究竟谁快——各种函数调用模式速度评测
查看>>
我到底要选择一种什么样的生活方式,度过这一辈子呢:人生自由与职业发展方向(下)...
查看>>
poj 题目分类
查看>>
windows 安装yaml支持和pytest支持等
查看>>
读书笔记:季羡林关于如何做研究学问的心得
查看>>
面向对象的优点
查看>>
套接口和I/O通信
查看>>
阿里巴巴面试之利用两个int值实现读写锁
查看>>
浅谈性能测试
查看>>
Winform 菜单和工具栏控件
查看>>
jequery动态创建form
查看>>
CDH版本大数据集群下搭建的Hue详细启动步骤(图文详解)
查看>>
第六次java作业
查看>>
巧用Win+R
查看>>