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.
- C++
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Haskell
- JS
- Java
- Java
- Java
- Kotlin
- Lisp
- Lua
- PHP
- Pascal
- Perl
- Python
- Ruby
- Rust
- VB
s.insert (s.begin () + i, x);
s.insert(i, x);
List.insert_at(s, i, x)
{Left, Right} = lists:split(I-1, S),
Left ++ [X|Right].
integer, dimension(:), allocatable :: s
s = [s(1:i-1), x, s(i:)]
take i s ++ x : drop i s
s.splice(i, 0, x);
int a = 0, n = s.length;
Class<?> c = s.getClass().getComponentType();
T t[] = (T[]) newInstance(c, n + 1);
arraycopy(s, a, t, a, i);
t[i] = x;
arraycopy(s, i, t, i + 1, n - i);
int a = 0, n = s.length,
t[] = new int[n + 1];
arraycopy(s, a, t, a, i);
t[i] = x;
arraycopy(s, i, t, i + 1, n - i);
(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))
array_splice($s, $i, 0, $x);
splice(@s, $i, 0, $x);
The 0 tells splice we're replacing zero elements with $x at position $i, resulting in an insertion rather than replacement. The problem does not specify if the position i is 0 or 1 based - so be careful when implementing in real code.
s.insert(i, x)
s.Insert(i,x)