Logo

Programming-Idioms

Insert the element x at position i in the list s. Further elements must be shifted to the right.
Implementation
Kotlin

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Kotlin implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
s = append(s, 0)
copy(s[i+1:], s[i:])
s[i] = x
import java.util.List;
s.add(i, x);
s.insert(i, x)
splice(@s, $i, 0, $x);
s.splice(i, 0, x);
s.insert(i, x);
array_splice($s, $i, 0, $x);
import std.array;
s.insertInPlace(i, x);
s.insert(i, x);
Uses Classes;
s.Insert(i, x)
s.insert(i, x)
List.insert_at(s, i, x)
take i s ++ x : drop i s
{Left, Right} = lists:split(I-1, S),
Left ++ [X|Right].
table.insert(s,i,x)
s.insert (s.begin () + i, x);
using System.Collections.Generic;
s.Insert(i, x);
integer, dimension(:), allocatable :: s

s = [s(1:i-1), x, s(i:)]
s.Insert(i,x)
import "slices"
s = slices.Insert(s, i, x)
(defun ins (lst x i)
 (if (zerop i) (cons x lst)
     (cons (car lst) (ins (cdr lst) x (- i 1)))))

(setf s (ins s x i))