How to work on containers and its functions?  

Scenario: By using container functions, here I am explaining how to add, modify, remove and search for email address. 

High Resolution steps 

Detailed Resolution steps 

Step 1: Create the runnable class to store the multiple Email-id’s in the container. 

Code Snippet: 

work on containers and its functions

Code: 

class ContainerExampleRunnable extends RunBase 

{ 

    public static void main(Args _args) 

    { 

        ContainerExampleRunnable example = new ContainerExampleRunnable(); 

        example.run(); 

    } 

    public void run() 

    { 

        container emailContainer; 

        int i,countmail; 

        str email; 

        // Add email IDs to the container 

        emailContainer = conIns(emailContainer, conLen(emailContainer) + 1, "user1@example.com"); 

        emailContainer = conIns(emailContainer, conLen(emailContainer) + 1, "user2@example.com"); 

        emailContainer = conIns(emailContainer, conLen(emailContainer) + 1, "user3@example.com"); 

        // Print the container content 

        //info("Email IDs in the container:"); 

        countmail = conLen(emailContainer); 

        for (i = 1; i <= countmail; i++) 

        { 

            email = conPeek(emailContainer, i); 

            info(email); 

        } 

 

        // Perform container operations 

        // Add another email ID 

        emailContainer = conIns(emailContainer, conLen(emailContainer) + 1, "user4@example.com"); 

        // Check container length 

        info(strFmt("Total emails: %1", conLen(emailContainer))); 

        // Remove the second email ID 

        emailContainer = conDel(emailContainer, 2 ,2); 

        // Modify an email using conPoke 

        emailContainer = conPoke(emailContainer, 2, "updated_user3@example.com"); 

        // Print container content after modification 

        info("Email IDs after modification:"); 

        countmail = conLen(emailContainer); 

        for (i = 1; i <= countmail; i++) 

        { 

            email = conPeek(emailContainer, i); 

            info(email); 

        } 

        // Find a specific email 

        if (conFind(emailContainer, "updated_user3@example.com") > 0) 

        { 

            info("updated_user3@example.com found in the container."); 

        } 

        else 

        { 

            info("updated_user3@example.com not found in the container."); 

        } 

        // Reset the container using conNull 

        emailContainer = conNull(); 

        // Check if container is empty 

        if (conLen(emailContainer) == 0) 

        { 

            info("The container is now empty."); 

        } 

    } 

}         

 

Step 2: Container Functions which is used to add, modify, remove, and search the elements within container. 

1. conIns(container, index, value) 

emailContainer = conIns(emailContainer, conLen(emailContainer) + 1, “user1@example.com”); 

Output: The container emailContainer now holds one email: “user1@example.com”. 

2. conLen(container) 

info(strFmt(“Total emails: %1”, conLen(emailContainer))); 

Output: The length of the container at this point will be 4, as four email addresses are inserted.  

3. conPeek(container, index) 

email = conPeek(emailContainer, i); 
info(email); 

Output: The email addresses in the container are printed. 

4. conDel(container, startIndex, count) 

emailContainer = conDel(emailContainer, 2 ,2); 

Output: The second email address (“user2@example.com”) is removed from the container. 

5. conPoke(container, index, value) 

emailContainer = conPoke(emailContainer, 2, “updated_user3@example.com”); 

Output: The email at index 2 is updated to “updated_user3@example.com”.  

6. conFind(container, value) 


if (conFind(emailContainer, "updated_user3@example.com") > 0) 

{ 

    info("updated_user3@example.com found in the container."); 

} 

else 

{ 

    info("updated_user3@example.com not found in the container."); 

} 

Output: Since the email is found, it will show in the container. 

7. conNull() 

emailContainer = conNull(); 

Output: It will display null values in the container. 

work on containers and its function

OUTPUT 

Once the runnable class is completed, 

it is creating, modifying, finding and deleting as shown in the below screenshot. 

Leave a Reply

Your email address will not be published. Required fields are marked *