What is an Array?

You are currently viewing What is an Array?

Overview

An Array is a fixed size and ordered collection of elements of the same data type. In other words, an array is a way to store multiple values in a single variable, instead of declaring a separate variable for each value. Arrays are commonly used in programming to organize data so that a related set of values can be stored together and be easily sorted or searched.

Below are the key characteristics of an array:

  • Fixed Size – The size of the array is determined at its initialization and cannot be changed.
  • Ordered Values – There is a specific order to the array. Elements don’t swap places within the array.
  • Same Data Type – All elements must be the same data type. An array of strings can only hold string variables.
  • Indexed – Each element in an array is given a number, called an index, starting at 0. You can use the index to access a specific element in the array based on their index number.

How to Create an Array in Uipath?

In UiPath, an array is seen as a set of curly brackets containing variables of the same data type separated by commas, for example:

Arrays can be assigned through the use of the Assign activity, as discussed here.

How to Access Elements in an Array?

You can access an element within an array by referring to its index number. 

In UiPath, this is accomplished by writing out the array name followed by the index number surrounded by parentheses. In the below example, we are looking to obtain the 3rd element in the array and assign it to a string variable.

To do this, we must write out the array variable name followed by the chosen index in parenthesis. In the below example we will be assigning it to a string variable named, strVar.

As you can see below, the output of the string Variable is now “Sly”.

How to Change the Value of an Array Element?

Even though the total number of elements in an array can’t change, that doesn’t mean the elements inside the array can’t change. Changing the value of an array element can be accomplished using the Assign activity in UiPath as shown below:

To do this, we must write out the array variable name followed by the chosen index we want to replace in parenthesis on the left side of the Assign activity. Then, on the right side of the assign activity, we write the string we want to replace it with, in this case “Tie”.

As you can see below, the output of the string Variable is now “Tie”.

What can you do with an Array?

Below are some great examples of the use of Arrays:

  • Looping through a group of similarly formatted items

Commonly Used Properties

The following are some commonly used properties for Arrays. A property is a characteristic or attribute of an object (i.e. Array). To learn more about properties, click here

For all examples below, we will be using the following Array of Strings, arrVar, which contains 3 elements and has been assigned as {“Hi”,”Bye”,”Sly”}. 

IsFixedSize - Indicates if the array has a fixed size. Always will be True.

Example Property Example Return Return Type
arrVar.IsFixedSize
True
Boolean
{4,3,5}.IsFixedSize
True
Boolean

IsReadOnly - Indicates if the array is read only. Always will be False.

Example Property Example Return Return Type
arrVar.IsReadOnly
False
Boolean
{"Hi","Bye"}.IsReadOnly
False
Boolean

Length - Gets the total number of elements in the array. Returns Int32.

Example Property Example Return Return Type
arrVar.Length
3
Integer (Int32)
{4,5,7,3}.Length
4
Integer (Int32)

LongLength - Gets the total number of elements in the array. Returns Int64.

Example Property Example Return Return Type
arrVar.LongLength
3
Integer (Int64)
{"Hi","Bye"}.LongLength
2
Integer (Int64)

Commonly Used Methods

The following are some commonly used methods for Arrays. Methods are actions that objects (i.e. Array) can perform. To learn more about methods, click here

For all examples below, we will be using the following Array of Strings, arrVar, which contains 3 elements and has been assigned as {“Hi”,”Bye”,”Sly”}. 

Clear(Array,Integer,Integer) - Removes elements in an array starting at an index for a certain length.

Example Method Example Return Return Type
Array.Clear({"Hi","Bye"},1,1)
{"Hi"}
Array
Array.Clear(arrVar,2,1)
{"Sly"}
Array

Clone - Creates a copy of the array.

Example Method Example Return Return Type
{4,3,5}.Clone
{4,3,5}
Array
arrVar.Clone
{"Hi","Bye","Sly"}
Array

Empty - Empties all of the elements from an array.

Example Method Example Return Return Type
{"Hi","Bye"}.Empty
{}
Array
arrVar.Empty
{}
Array

GetValue(Integer) - Gets the value at a specified index.

Example Method Example Return Return Type
{4,3,5}.GetValue(0)
4
Object
arrVar.GetValue(2)
{"Sly"}
Object

Contains(Object) - Checks if a specified object exists in an array.

Example Method Example Return Return Type
{"Cat","Dog"}.Contains("Dog")
True
Boolean
arrVar.Contains("405")
False
Boolean

IndexOf(Array,Object) - Returns the index of an object within a specified array.

Example Method Example Return Return Type
Array.IndexOf({4,3,5},5)
2
Integer (Int32)
Array.IndexOf(arrVar,"Hi")
0
Double