Kotlin ArrayListOf

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

In Kotlin, an ArrayList, specifically created using the arrayListOf function, is a dynamic array that can store a collection of elements. It's flexible and allows for adding, modifying, and removing items efficiently. You can declare and initialize a Kotlin arrayListOf with a specific data type, making it a versatile tool for managing lists of elements in your code.

Syntax

1.Creating an Empty ArrayList:

  1. Creating an ArrayList with Provided Elements:
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Examples

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

1. Kotlin Program To Make New Empty ArrayList

Code:

Output:

2. Kotlin Program To Make New ArrayList with String Elements

Code:

Output :

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

3. Kotlin Program To Make New ArrayList With Elements Of Any Data Type

Code :

Output:

Functions of ArrayListOf

  • add(element):
    This function is used to add a single element to the end of an ArrayList.

Code:

Output:

  • add(index, element):
    This function is used to insert a single element at a specific index within an ArrayList.

Code:

Output:

  • addAll(elementCollection):
    This function is used to add all elements from another collection to the end of an ArrayList

Code :

Output:

  • addAll(index, elementCollection):
    This function is used to add all elements from another collection to a specific index within an ArrayList.

Code:

Output:

  • clear():
    The clear method is used to remove all the elements from an ArrayList, effectively making it an empty ArrayList.

Code:

Output:

  • contains(element):
    This function is used to check whether a specific element exists in an ArrayList. It returns true if the element is found in the ArrayList, and false otherwise.

Code:

Output:

  • containsAll(elementCollection):
    This function is used to check if all elements in the specified collection are found in the ArrayList and returns true if so; otherwise, it returns false.

    Code:

Output :

  • get(index):
    This function retrieves an element at a specific index in an ArrayList.

    Code:

Output:

  • indexOf(element) :
    This function returns the index of the first occurrence of an element in an ArrayList, or -1 if the element is not found.

    Code:

Output:

  • lastIndexOf(element):
    This function returns the index of the last occurrence of an element in an ArrayList, or -1 if the element is not found.

Code:

Output:

  • remove(element) :
    This function removes an element from an ArrayList and returns true if successful, false if the element is not found.

Code:

Output:

  • removeAll(elementCollection) :
    This function removes elements from an ArrayList and returns true if any elements were removed, or false if none were found.

Code:

Output:

  • removeAt(index):
    removes an element by its position in an ArrayList, returning true if the element is successfully removed, or false if not found.

Code:

Output:

  • set(index, element):
    This function updates the element at a specific position in an ArrayList.

Code:

Output:

  • toArray():
    This function converts an ArrayList into an array of a specific type.

Code:

Output:

  • toString():
    This function is used to obtain the string representation of an ArrayList object.

Code:

Output:

  • isEmpty() :
    This function is used to check if an ArrayList is empty.

    Code:

Output:

Conclusion

  1. ArrayList in Kotlin is a dynamic, flexible data structure that allows you to store and manage collections of elements efficiently.
  2. You can create an ArrayList using the arrayListOf() function, which can be initially empty or contain specific elements.
  3. Kotlin arrayListOf is versatile and supports various operations, including adding, inserting, removing, and updating elements, making them a powerful tool for list management.
  4. Key functions such as add, addAll, remove, set, and others provide convenient ways to manipulate the content of an ArrayList.
  5. You can also check for the presence of elements, find their indices, convert a Kotlin arrayListOf to an array, and check if it's empty, making Kotlin arrayListOf a comprehensive tool for handling lists in Kotlin.