Arrays and Arraylist
Learn about arrays in Powershell and Python along with Arraylist in Powershell.
We'll cover the following...
Arrays
A PowerShell array is an immutable (fixed sized) data structure that can hold heterogeneous elements.
Arrays in Powershell are implicitly typed, but if we can cast the variable as an array type if we want to create a strongly typed array, such as string[]
, long[]
, or int32[]
.
Press + to interact
## creating arrays$array = 1,2,3,4,5 # homogeneous arrayWrite-Host $array$array = 1, 2, 'text', 1.5 # heterogeneous arrayWrite-Host $array$array.gettype() # get data type of the array[int[]] $array = 1,2,3,4,5 # String typed [int[]] arrayWrite-Host $array## Strongly typed array can cast different data types of elements to the defined data type## if the conversion is possible like [double] to [int] or [char] to [int][int[]] $array = 1,2.1,3,4.3,5,[char]'a'Write-Host $array## throws error 'Cannot convert value "text" to type "System.Int32".'## because [string] can't be type casted to [int][int[]] $array = 1, 2, 'text', 1.5
Python doesn’t have a native array data structure, but arrays are supported using the built-in module called array
. This module needs to be imported before using the data structure. The elements stored in a Python array are restricted by their data type and thus are homogeneous in nature. This means a character array can only store character elements, unlike ...