10/30/2023 0 Comments Kotlin arraylistint lastIndexOf(Object o) It returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element. boolean isEmpty() It returns true if the list is empty, otherwise false. E get(int index) It fetches the element from the particular position of the list. void ensureCapacity(int requiredCapacity) It enhances the capacity of an ArrayList instance. void clear() It removes all of the elements from this list. boolean addAll(int index, Collection c) It appends all the elements in the specified collection, starting at the specified position of the list. boolean addAll(Collection c) It appends all of the elements in the specified collection to the end of this list. boolean add(E e) It appends the specified element at the end of a list. Method Description void add(int index, E element) It inserts the specified element at the specified position in a list. We can use them to manipulate its elements. The below table contains methods of Arraylist. Here list is empty because we did not add elements to it. Lets create an ArrayList to store string elements. ArrayList ConstructorsĪrrayList class has three constructors that can be used to create Arraylist either from empty or elements of other collection.ĪrrayList() // It creates an empty ArrayListĪrrayList( Collection C ) // It creates an ArrayList that is initialized with elements of the Collection CĪrrayList( int capacity ) // It creates an ArrayList that has the specified initial capacity ArrayList allows random access because it works on the index basis.Manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.It can contain Duplicate elements and it also maintains the insertion order.ArrayList supports dynamic array that can grow as needed.Public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable It creates a dynamic array that grows based on the elements strength. This class implements List interface and an abstract AbstractList class. This class provides implementation of an array based data structure that is used to store elements in linear order.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |