Using Array Elements

Using Array Elements

Now, the elements can be accessed by their indices (plural of index).

Examples:

Processing:

println(shoppingList); 	// Prints full array

println("My shopping list includes: " + shoppingList[0] + ", " + shoppingList[1] + 
", " + shoppingList[2] + ", " + shoppingList[3] + ", and " + shoppingList[4] + 
".");
 
// Prints "My shopping list includes: milk, bread, eggs, bottled water, and soda."

Adding Elements to an Array

Sometimes you need to add elements to an array. Often, you’ll get an error if you try to assign a value to a non-existent “slot” or reference an index outside of what is available.

Most languages use a function such as .append() or .expand() or .push() or .add()

Examples:

Processing:

Removing Elements from an Array

Making an index / element empty does not remove the element.

To remove the element, use a function such as .shorten() (Processing) or .Remove() (Unity/C#).

Examples:

Processing:

Sorting Arrays

To sort an array, use a function such as .sort() for alphabetical OR .reverse() for the opposite.

Examples:

Processing:

Last updated

Was this helpful?