Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #44 Insert element in list

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

s.insert (s.begin () + i, x);
using System.Collections.Generic;
s.Insert(i, x);
import std.array;
s.insertInPlace(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:)]
import "slices"
s = slices.Insert(s, i, x)
s = append(s, 0)
copy(s[i+1:], s[i:])
s[i] = x
take i s ++ x : drop i s
s.splice(i, 0, x);
import static java.lang.System.arraycopy;
import static java.lang.reflect.Array.newInstance;
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);
import static java.lang.System.arraycopy;
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);
import java.util.List;
s.add(i, x);
s.add(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))
table.insert(s,i,x)
array_splice($s, $i, 0, $x);
Uses Classes;
s.Insert(i, x)
splice(@s, $i, 0, $x);
s.insert(i, x)
s.insert(i, x)
s.insert(i, x);
s.Insert(i,x)

New implementation...
s.insert (s.begin () + i, x);
s.Insert(i, x);
s.insertInPlace(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:)]
s = slices.Insert(s, i, x)
s = append(s, 0)
copy(s[i+1:], s[i:])
s[i] = x
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);
s.add(i, x);
s.add(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))
table.insert(s,i,x)
array_splice($s, $i, 0, $x);
s.Insert(i, x)
splice(@s, $i, 0, $x);
s.insert(i, x)
s.insert(i, x)
s.insert(i, x);
s.Insert(i,x)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /