Button handy |
---|
blank | true |
---|
color | #0052CC |
---|
name | Send Feedback |
---|
link | https://docs.google.com/forms/d/e/1FAIpQLScmToBe3vynAlb5fdKwCGxYqnTbDc66sIBgeecG2BuFDuHc7g/viewform?entry.2002826954=Use+Case+Examples+-+15480840 |
---|
width | auto |
---|
|
...
Putting some of the core concepts together to perform some common examples based on the use case.
...
This section combines core array concepts to demonstrate practical use cases and examples. |
Example 1 - Empty array declaration Code Block |
---|
string [] fruit; |
| Creates an uninitialized array with no elements. The array exists but contains no values. |
Example 2 - Array with predefined values Code Block |
---|
string [] fruit = {"Apple", "Apricot", "Banana", "Cherry"}; |
| Initializes an array with a fixed set of values, specifying the elements directly during declaration. |
Example 3 - Array from delimited string Code Block |
---|
string [] produce = "Apple|Apricot|Avocado|Banana|Blackberry|Blueberry|Cherry|Coconut"; |
| Leverages the language's automatic conversion of pipe-delimited strings into an array. This method provides a concise way to create arrays from text-based lists. |
Array keys
Using a key-value pair lets you create key-value lists (maps) and retrieve values from them by using a string inside the operator (instead of a number).
Example 1 - Add values by key Code Block |
---|
string [] fruitColor;
fruitColor["Apple"] = "Red";
fruitColor["Banana"] = "Yellow";
fruitColor["Orange"] = "Orange"; |
| Creates an associative array where each fruit is a key linked to its corresponding color. This approach creates flexible, named collections where elements can be accessed by meaningful identifiers instead of numeric indexes. |
Example 2 - Retrieve values by key Code Block |
---|
return fruitColor["Banana"]; |
| Retrieves the value associated with a specific key. In this example, it would return "Yellow". This method provides direct, readable access to stored values using their unique identifiers. |
Array values
Any variable type can be transformed into an array by adding the array symbols []
after the type declaration.
Example 1 - Add values by a specific index Code Block |
---|
string [] fruit;
fruit[0] = "Apple";
fruit[1] = "Apricot";
fruit[3] = "Banana"; |
| Directly assigns values to specific array positions using numeric indexes. This method allows precise control over element placement, with the ability to leave gaps between assigned values. Note that unassigned indexes may remain undefined. |
Example 2 - Add values dynamically Code Block |
---|
string [] fruit;
fruit += "Apple";
fruit += "Apricot";
fruit += "Banana"; |
| Adds elements to the end of the array sequentially. This approach automatically manages array expansion, appending each new value to the next available position without manually tracking indexes. |
Example 3 - Retrieve a specific value Code Block |
---|
return fruit[1]; |
| Accesses a specific array element using its numeric index. This example would return "Apricot,” the second element in the array (remembering that array indexing typically starts at 0). |