Add, access, and remove items from a List of strings.
← Collections and Data Structures · Ref: Q1160
List operations use operators and methods:
names <- List() of String names += "Alice" names += "Bob" stdout.println($ length names) cat names > stdout
+= adds items. 'length' prefix operator returns size. 'cat list > stdout' prints all items.
See Q1158 for Dict iteration. See Q1081 for list merge.
Example
defines module qa.collections.listcomprehensive defines program ListComprehensiveDemo() stdout <- Stdout() names <- List() of String names += "Alice" names += "Bob" names += "Charlie" //Length stdout.println(`Length: ${$ length names}`) //Print all items cat names > stdout //List literal shorthand numbers <- [10, 20, 30] stdout.println(`Numbers: ${numbers}`)
Common mistakes
E50060 — EK9 uses += operator to add items to a list, not .add() method.
Incorrect:
names.add("Alice")
Correct:
names += "Alice"
Other ways to ask this
- I need to perform basic List operations: add, get, remove, check length
- In Java I'd use ArrayList methods. Write the EK9 List equivalents
- Given a List of names, demonstrate add with +=, access, and length
- Show the core List operations on a collection of strings
Coming from another language?
Java: list.add(), list.size(), list.get(). Python: list.append(), len(list). EK9: += to add, length prefix, cat to iterate.
Keywords: operations, access, length, List, add, remove