zoukankan      html  css  js  c++  java
  • [Powershell] Powershell dynamic arrays

    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.

  • 相关阅读:
    O(logn)的乘方算法
    求递归算法时间复杂度:递归树
    Xcode6 创建静态库
    Intellij IDEA使用总结
    使用Entity Framework迁移完数据库后,每次修改代码(非模型代码)后都报错。
    Entity Framework Code First数据库自动更新
    Java中 堆 栈,常量池等概念解析(转载)
    使用MyEclipse9.0开发Web Service
    (转) Java源码阅读的真实体会
    Hibernate注解版设计学生、课程、成绩的关系映射
  • 原文地址:https://www.cnblogs.com/LarryAtCNBlog/p/4252464.html
Copyright © 2011-2022 走看看