Kotlin array delete element

ArrayList

Provides a MutableList implementation, which uses a resizable array as its backing storage.

This implementation doesn’t provide a way to manage capacity, as backing JS array is resizeable itself. There is no speed advantage to pre-allocating array sizes in JavaScript, so this implementation does not include any of the capacity and «growth increment» concepts.

Constructors

Creates an ArrayList filled from the elements collection.

Properties

size

Functions

add

Adds the specified element to the end of this list.

Inserts an element into the list at the specified index.

addAll

Adds all of the elements of the specified collection to the end of this list.

Inserts all of the elements of the specified collection elements into this list at the specified index.

clear

Removes all elements from this collection.

contains

containsAll

ensureCapacity

Does nothing in this ArrayList implementation.

equals

Indicates whether some other object is «equal to» this one. Implementations must fulfil the following requirements:

get

Returns the element at the specified index in the list.

hashCode

Returns a hash code value for the object. The general contract of hashCode is:

indexOf

Returns the index of the first occurrence of the specified element in the list, or -1 if the specified element is not contained in the list.

isEmpty

iterator

lastIndexOf

Returns the index of the last occurrence of the specified element in the list, or -1 if the specified element is not contained in the list.

listIterator

Returns a list iterator over the elements in this list (in proper sequence).

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified index.

remove

Removes a single instance of the specified element from this collection, if it is present.

removeAll

Removes all of this collection’s elements that are also contained in the specified collection.

removeAt

Removes an element at the specified index from the list.

removeRange

Removes the range of elements from this list starting from fromIndex and ending with but not including toIndex.

retainAll

Retains only the elements in this collection that are contained in the specified collection.

set

Replaces the element at the specified position in this list with the specified element.

subList

Returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.

toArray

toString

Returns a string representation of the object.

trimToSize

Does nothing in this ArrayList implementation.

Inherited Functions

containsAll

isEmpty

Extension Properties

indices

Returns an IntRange of the valid indices for this collection.

lastIndex

Returns the index of the last item in the list or -1 if the list is empty.

Extension Functions

addAll

Adds all elements of the given elements collection to this MutableCollection.

Adds all elements of the given elements sequence to this MutableCollection.

Adds all elements of the given elements array to this MutableCollection.

all

Returns true if all elements match the given predicate.

any

Returns true if collection has at least one element.

Returns true if at least one element matches the given predicate.

asIterable

Returns this collection as an Iterable.

asSequence

Creates a Sequence instance that wraps the original collection returning its elements when being iterated.

associate

Returns a Map containing key-value pairs provided by transform function applied to elements of the given collection.

associateBy

Returns a Map containing the elements from the given collection indexed by the key returned from keySelector function applied to each element.

Returns a Map containing the values provided by valueTransform and indexed by keySelector functions applied to elements of the given collection.

associateByTo

Populates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function applied to each element of the given collection and value is the element itself.

Populates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function and and value is provided by the valueTransform function applied to elements of the given collection.

fun < T , K , V , M : MutableMap < in K , in V >> Iterable < T >. associateByTo (
destination : M ,
keySelector : ( T ) -> K ,
valueTransform : ( T ) -> V
) : M

associateTo

Populates and returns the destination mutable map with key-value pairs provided by transform function applied to each element of the given collection.

associateWith

Returns a Map where keys are elements from the given collection and values are produced by the valueSelector function applied to each element.

associateWithTo

Populates and returns the destination mutable map with key-value pairs for each element of the given collection, where key is the element itself and value is provided by the valueSelector function applied to that key.

binarySearch

Searches this list or its range for the provided element using the binary search algorithm. The list is expected to be sorted into ascending order according to the specified comparator, otherwise the result is undefined.

fun < T > List < T >. binarySearch (
element : T ,
comparator : Comparator < in T >,
fromIndex : Int = 0 ,
toIndex : Int = size
) : Int

Searches this list or its range for an element for which the given comparison function returns zero using the binary search algorithm.

fun < T > List < T >. binarySearch (
fromIndex : Int = 0 ,
toIndex : Int = size ,
comparison : ( T ) -> Int
) : Int

binarySearchBy

Searches this list or its range for an element having the key returned by the specified selector function equal to the provided key value using the binary search algorithm. The list is expected to be sorted into ascending order according to the Comparable natural ordering of keys of its elements. otherwise the result is undefined.

fun < T , K : Comparable < K >> List < T >. binarySearchBy (
key : K ? ,
fromIndex : Int = 0 ,
toIndex : Int = size ,
selector : ( T ) -> K ?
) : Int

chunked

Splits this collection into a list of lists each not exceeding the given size.

Splits this collection into several lists each not exceeding the given size and applies the given transform function to an each.

Источник

Collection write operations

Mutable collections support operations for changing the collection contents, for example, adding or removing elements. On this page, we’ll describe write operations available for all implementations of MutableCollection . For more specific operations available for List and Map , see List-specific Operations and Map Specific Operations respectively.

Adding elements

To add a single element to a list or a set, use the add() function. The specified object is appended to the end of the collection.

addAll() adds every element of the argument object to a list or a set. The argument can be an Iterable , a Sequence , or an Array . The types of the receiver and the argument may be different, for example, you can add all items from a Set to a List .

When called on lists, addAll() adds new elements in the same order as they go in the argument. You can also call addAll() specifying an element position as the first argument. The first element of the argument collection will be inserted at this position. Other elements of the argument collection will follow it, shifting the receiver elements to the end.

You can also add elements using the in-place version of the plus operator — plusAssign ( += ) When applied to a mutable collection, += appends the second operand (an element or another collection) to the end of the collection.

Removing elements

To remove an element from a mutable collection, use the remove() function. remove() accepts the element value and removes one occurrence of this value.

For removing multiple elements at once, there are the following functions :

  • removeAll() removes all elements that are present in the argument collection. Alternatively, you can call it with a predicate as an argument; in this case the function removes all elements for which the predicate yields true .
  • retainAll() is the opposite of removeAll() : it removes all elements except the ones from the argument collection. When used with a predicate, it leaves only elements that match it.
  • clear() removes all elements from a list and leaves it empty.

fun main() < //sampleStart val numbers = mutableListOf(1, 2, 3, 4) println(numbers) numbers.retainAll < it >= 3 > println(numbers) numbers.clear() println(numbers) val numbersSet = mutableSetOf(«one», «two», «three», «four») numbersSet.removeAll(setOf(«one», «two»)) println(numbersSet) //sampleEnd >

Another way to remove elements from a collection is with the minusAssign ( -= ) operator – the in-place version of minus . The second argument can be a single instance of the element type or another collection. With a single element on the right-hand side, -= removes the first occurrence of it. In turn, if it’s a collection, all occurrences of its elements are removed. For example, if a list contains duplicate elements, they are removed at once. The second operand can contain elements that are not present in the collection. Such elements don’t affect the operation execution.

Updating elements

Lists and maps also provide operations for updating elements. They are described in List-specific Operations and Map Specific Operations. For sets, updating doesn’t make sense since it’s actually removing an element and adding another one.

Источник

Remove an item from Array in Kotlin

In this post, we are going to explain different methods that can be used to delete elements from an array using Kotlin. We will be converting the array to a mutable list to delete items from array.

fun removeElement(arr: Array, itemIndex: Int): Array  < val arrList = arr.toMutableList() arrList.removeAt(itemIndex) return arrList.toTypedArray() >fun main() < var fruits = arrayOf("Apple", "Banana", "Orange", "Papaya") // Remove second element from array fruits = removeElement(fruits, 2) println(fruits.contentToString()) >

We have created an array called fruits that contain multiple items. We want to delete an item that has an index 2. To remove the item we have created a function named ‘removeElement‘. In this function, we are taking two parameters. First is the array from where you want to delete the item. The second is the index of the item that needs to be removed.

We are converting the array to a mutable list using the .toMutableList() function and using removeAt() function of the mutable list, we are removing the item.

Remove an item from Mutable List in Kotlin

If we have a mutable list then we can easily remove the item from it. The mutable list has a function removeAt() that takes the index as a parameter and removes that item from the Mutable List.

fun main() < val mList = mutableListOf(10, 20, 30, 40, 50, 60) mList.removeAt(3) println(mList) >

Источник

Читайте также:  Css select class that starts with
Оцените статью