How to insert a value into a array

i am trying to insert the value into the first index of array

tried to use $a.Insert(0,1)
but give an error of

MethodInvocationException: Exception calling "Insert" with "2" argument(s): "Collection was of a fixed size."

i don’t know if this the right mehod or am i pass the wrong overload

I believe it depends on how you defined/cast your array. Try defining your array as such

[System.Collections.ArrayList]$a = @('yo1', 'yo2', 'yo3')
or
$a = System.Collections.ArrayList]('yo1', 'yo2', 'yo3')

Then, this should work:

$a.Insert(0, "yo4")

Although when you define your array as:

$a = @('yo1', 'yo2', 'yo3', 'yo4')
and enter
$a | Get-Member

The Insert method is certainly visible, but when used, casts the error you show. Wondering if someone can explain in more detail?

Arrays, as opposed to collections like Arraylists, are fixed size. See here:

It’s a little less straight forward to inspect the members of an Array object because if you just pipe your array to Get-Member you get information about the objects from within the array.

But try this.

$array = @()
$array.psobject.members | select name,membertype

You should get output like this:

Name                          MemberType
----                          ----------
Length                          Property
LongLength                      Property
Rank                            Property
SyncRoot                        Property
IsReadOnly                      Property
IsFixedSize                     Property
IsSynchronized                  Property
Item               ParameterizedProperty
Count                           Property
Get                               Method
Set                               Method
Address                           Method
get_Length                        Method
get_LongLength                    Method
get_Rank                          Method
GetLength                         Method
GetUpperBound                     Method
GetLowerBound                     Method
Initialize                        Method
GetValue                          Method
SetValue                          Method
GetLongLength                     Method
get_SyncRoot                      Method
get_IsReadOnly                    Method
get_IsFixedSize                   Method
get_IsSynchronized                Method
Clone                             Method
CopyTo                            Method
GetEnumerator                     Method
GetType                           Method
ToString                          Method
Equals                            Method
GetHashCode                       Method
get_Item                          Method
set_Item                          Method
Add                               Method
Contains                          Method
Clear                             Method
IndexOf                           Method
Insert                            Method
Remove                            Method
RemoveAt                          Method
get_Count                         Method
CompareTo                         Method

Notice that “IsFixedSize” property? Try calling that.
If you check the Microsoft page on the System.Array you’ll see they also remark that these Arrays are fixed size and that the classes in the System.Collections namespaces can be resized.

Very good to know. Thanks for the explanation :slight_smile:

thx for the answer ,for what i know if an array was create from foreach-object ,i will be the fixed size array.
And to insert value you should first turn it into none fixed size

$a=(1,2,3)
$a=[System.Collections.ArrayList]$a
$a.Insert(1,1)

At that point you could use the += syntax of adding to an array BUT you lose the ability to choose the index.
In your last example it’s likely tearing down the array and rebuilding it into an arraylist, so a similar performance hit to the += method, but if you’re only doing it occasionally not a big deal.
What I would wonder about is if there’s any performance benefit to just using the Add method in your loop against the collection instead of capturing the output in an variable, resulting in a fixed sized array.

There’s a distinct difference between common arrays and arraylists. Doing …

… you destroy the one creating the other one.

This may help understanding arrays a little more:

Do you actually have a specific issue you want to solve? Or is it just to understand how it works? There may be another / better way anyway!? :man_shrugging:t3:

i am working with a roads path

mycoordinate to the another placeA is directed
and placeA to the place B is undirected
so i figure i could first get all the posible path like
A -B
B-A
and i add the my coordinate into the first index so it look like
O-A-B
O-B-A

I’m not sure I’m following your examples completely here, but even so; what about using properties?

[PSCustomObject]@{
    Coordinate = "127, -46"
    Location1   = "A"
    Location2   = "B"
    Location3   = $null
}

This way if “Coordinate” was nothing, you can just leave it as $null, but later if you wanted to add value to it you’d just call the variable containing your object, reference the property, and set it.
$MyObject.Coordinate = "47,-122" or whatever it should be.

If that doesn’t work for your use case then I would definitely stick with something from the Collections namespace like an Arraylist. But think about objects, or maybe a hashtable could help here too if you’ve got a unique key for everything.