List
It is used to create a list of objects. Similar to arrays, but can be added and removed after creation.
Usage: List<ClassName> list=new List<ClassName>();
Example 1:
Let's create a products list. Let's add, remove and fetch. Finally, let's print entire list.
mainfunc(){
//Let's create a Text List named "products".
Liste<Text> products=new List<Text>();
//Let's add products to list.
products.add("milk");
products.add("egg");
products.add("bread");
products.add("cheese");
products.add("olive");
products.add("jam");
//Let's remove first element of list.
products.remove(0);
//Let's remove the "jam" element.
products.remove("jam");
//Let's print the last element of the list.
print(products.get(products.size-1));
print("Products List");
//Let's print elements of list with spaces between them.
print(Text.join("\n"t, products));
}
Output:
olive
Products List
egg
bread
cheese
olive
Example 2:
Let's make a Product List and print it using Product class we used in Classes section.
Product.a
public Text name;
public Number price;
init(Text name, Number price){
this.name=name;
this.price=price;
}
public func info() print("Name: {name}, Price: {price}");
MainClass.a
mainfunc(){
// Let's create a Product List named "products".
List<Product> products=new List<Product>();
// Let's add new Products to the "products" list.
products.add(new Product("Pencil", 10));
products.add(new Product("Eraser", 5));
products.add(new Product("Notebook", 25));
products.add(new Product("Bag", 100));
print("Products");
// Let's travel "products" list and print information of products in list.
loop(Product product:products) product.info();
}
Output:
Products
Name: Pencil, Price: 10.0
Name: Eraser, Price: 5.0
Name: Notebook, Price: 25.0
Name: Bag, Price: 100.0
Error Checking
Errors may occur when program starts or after it starts. When an error occurs, program is usually terminated. Code is tested with error checking, and when there is an error, other codes can be run.
Usage: try{<Code>}catch(Error Error){<When Error>}
Example:
Let's create a "notes" List. Let's add 2 notes. Let's print first 5 notes. It will give an error because there are not 5 notes. Let's check it out.
mainfunc(){
// Let's create a list called "notes".
List<Text> notes=new List<Text>();
// Let's add our notes.
notes.add("Pay bills");
notes.add("Weekly shopping");
// Let's print our first 5 notes.
print("First 5 Notes");
try{
loop(Int i=0; i<5; i++) print(notes.get(i));
// It will not print first 5 notes because there are 2 notes.
}
catch(Error error){
print("Note not found");
print("Error: {error}");
// Print Error.
}
finally{
print("Process completed");
// Try and catch processes are complete.
}
}
Output:
First 5 Notes
Pay bills
Weekly shopping
Note not found
Error: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
Process completed
Let's make our own mistake.
Example:
Let's set an age limit. If it is below given age limit, it will return an error.
mainfunc(){
Int age=10;
try{
if(age<18) returnError("Access Denied");
else{
print("Welcome");
}
}
catch(Error error) print("Error: {error}");
}
Output:
Error: java.lang.Exception: Access Denied