$newArray = @()
$newArray.Add("Hello")

  

If I create a new array, and using the method Add(). Windows PowerShell will tell me :

Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."

Reason:

When you use the $array.Add() method, you're trying to add the element into the array. An array is a collection of fixed size, so you will recieve an error.

So, what should I do ?

Solution 1:

$successfulArray = New-Object System.Collections.Generic.List[System.Object]

$successfulArray.Add("Hello")
$successfulArray.Add("World") # When you need array, you can transfer like:
$successfulArray.ToArray()

  

Also a more simple solution 2:

$easyArray = @()
$easyArray += "Hello"
$easyArray += "World"

  PS creates a NEW array with the same elements as $array+ the one(s) you're adding, and then it overwrites the original.

Array.Add () and += in PowerShell的更多相关文章

  1. 448. Find All Numbers Disappeared in an Array Add to List

    题目描述 题目分析 有个[1,n]的条件要充分利用起来. 题目代码 public class Solution { public List<Integer> findDisappeared ...

  2. 使用powershell提权的一些技巧

    原文:http://fuzzysecurity.com/tutorials/16.html 翻译:http://www.myexception.cn/windows/1752546.html http ...

  3. [PowerShell] 快速入门, 基本语法, 常用类型, 函数, .NET 互操作

    PowerShell 快速入门 开始之前, 我们认定你已经有一定的编程基础, 熟悉 .NET 中的类型与对象. 此文章对于 .NET 开发者来说更简单哦! 在 PowerShell 中, 几乎一切都是 ...

  4. 作为平台的Windows PowerShell(一)

    除了作为一种脚本语言外,Windows PowerShell被多种应用程序使用.这是因为Windows PowerShell引擎可以被托管在一个应用程序内部.这篇博文和下一篇博文将会处理在C#应用程序 ...

  5. [置顶] Array ArrayList LinkList的区别剖析

    这是一个面试中我们经常被问到的问题 Array.ArrayList.LinkList之间的区别:Array.ArrayList.LinkList均属于泛型的范畴,都用来存放元素,主要区别是Array是 ...

  6. Powershell创建数组

    在Powershell中创建数组可以使用逗号. PS C:Powershell> $nums=2,0,1,2 PS C:Powershell> $nums 2 0 1 2 对于连续的数字数 ...

  7. LeetCode 525. Contiguous Array

    525. Contiguous Array Add to List Description Submission Solutions Total Accepted: 2476 Total Submis ...

  8. PowerShell 字符串操作--转载

    格式化操作符 –F 在PowerShell文本操作符中非常重要,经常被用来增强数字类型和日期类型的可读性: "{0} diskettes per CD" -f (720mb/1.4 ...

  9. 9.1 ArrayList(集合)的使用,与array(数组)的对比

    1.array 和ArrayList的区别? array 数组的长度是固定的,适应不了变化的需求. ArrayList集合的长度可变.大小可变. 2.为什么要用集合,它优点是什么? java是面向对象 ...

随机推荐

  1. SPOJ QTREE4 lct

    题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...

  2. DataUml Design 教程6-DataUML Design 1.1版本号正式公布(支持PD数据模型)

    从DataUML Design正式公布到如今有两个月了.因为近期比較忙,到如今才公布1.1版本号. 以后本人会一直坚持不断完好DataUML Design软件,希望广大程序员们多多支持. 一.1.1版 ...

  3. 基于注解的EF

    首先得你的ef dll版本在4.1以上 第一步贴第一个类 由于字段太多就写一部分  [Table("NavF")]//设置表名称     public class NavF     ...

  4. 简单的Session登录

    Login前台页面 <form id="form1" action ="" method="post"> <input t ...

  5. JavaScrtip之JS最佳实践

    一.JavaScript之平稳退化 这边使用一个当用户点击某个页面内某个链接弹出一个新窗口的案例: JavaScript使用window对象的open()方法来创建新的浏览器窗口; window.op ...

  6. HDU 5584 LCM Walk(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意:(x, y)经过一次操作可以变成(x+z, y)或(x, y+z)现在给你个点(ex, e ...

  7. table操作:边框-斑马线-多表头-焦点高亮-自动求和

    一.操作table,本例子实现的功能: 1.table等宽边框2.table斑马线3.实现table多表头4.焦点所在行高亮5.自动计算总分 二.效果图 三.代码: <!DOCTYPE html ...

  8. Python 错误和异常

    1.Python异常类 Python是面向对象语言,所以程序抛出的异常也是类.常见的Python异常有以下几个,大家只要大致扫一眼,有个映像,等到编程的时候,相信大家肯定会不只一次跟他们照面(除非你不 ...

  9. 搭建Ubuntu环境中的Error [dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此问题][安装Flashplayer出错 ]

    //解决方法如下: sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock sudo dpkg -r flashplugin-i ...

  10. JConsole是什么

    从Java 5开始 引入了 JConsole.JConsole 是一个内置 Java 性能分析器,可以从命令行或在 GUI shell 中运行.您可以轻松地使用 JConsole(或者,它更高端的 “ ...