Arrays in Artsy

An array is a data structure that allows you to store a collection of elements of the same data type under one variable name. Each element in an array can be accessed by its index (position) within the array.

Declaring an Array

To declare an array, you use the declare keyword followed by the array name, the data type of the array elements enclosed in square brackets, and the size of the array in parentheses.

# Declare an integer array with 5 elements
declare numbers as int[5]

# Declare a string array with 3 elements
declare colors as string[3]

Initializing an Array

You can initialize an array by assigning a value to each element in the array using the assignment operator =.

# Initialize an integer array with values
declare numbers as int[5];                                    # (Working Progress)          
let numbers[0] = 10;
let numbers[1] = 20;
let numbers[2] = 30;
let numbers[3] = 40;
let numbers[4] = 50;

# Initialize a string array with values (WIP)                 # (Working Progress)
declare colors as string[3]                                   # (Working Progress)
let colors[0] = "red";
let colors[1] = "green";
let colors[2] = "blue";

Accessing Array Elements

To access an element in an array, you use the array name followed by the index of the element in square brackets.

# Access elements in an integer array
declare numbers as int[5]
let numbers[0] = 10;
let numbers[1] = 20;
let numbers[2] = 30;
let numbers[3] = 40;
let numbers[4] = 50;

print numbers[0]; # Output: 10
print numbers[2]; # Output: 30

# Access elements in a string array (WIP)         # (Working Progress)
declare colors as string[3]                       # (Working Progress)
let colors[0] = "red";                            # (Working Progress)
let colors[1] = "green";
let colors[2] = "blue";

print colors[1]; # Output: green (Working Progress)   # (Working Progress)
print colors[2]; # Output: blue (Working Progress)    # (Working Progress)

Looping through an Array

You can use a while loop to iterate through all the elements in an array.

# Loop through an integer array
declare numbers as int[5]
let numbers[0] = 10;
let numbers[1] = 20;
let numbers[2] = 30;
let numbers[3] = 40;
let numbers[4] = 50;

declare i as int;
let i = 0;
while (i < 5) {
    print numbers[i];                                        # (Working Progress)
		let i = i + 1;
}

# Loop through a string array (Working Progress)             # (Working Progress)
declare colors as string[3]   (Working Progress)             # (Working Progress)
let colors[0] = "red";                                       # (Working Progress)
let colors[1] = "green";                                     # (Working Progress)
let colors[2] = "blue";                                      # (Working Progress)

let i = 0;
while (i < 3)  {
    print colors[i];  (Working Progress)                     # (Working Progress)
		let i = i + 1;
}

Conclusion

In conclusion, arrays are an essential data structure in programming that allow for the efficient storage and manipulation of multiple values of the same type. They provide a convenient and flexible way to access and modify elements within the collection, and are supported in many programming languages, including Artsy.

By understanding the basics of array syntax, including declaration, initialization, and accessing elements, you can begin to incorporate arrays into your programs and take advantage of their many benefits. With practice, you can learn to use arrays effectively to solve a wide range of programming problems and improve the performance and readability of your code.

Return to Homepage

Artsy Coding