配列の指定した位置に値を挿入する Array.insertAt
ひとことで言うと
指定された位置に値を挿入し、長さを 1 つ増やします。
ブロックとコード
配列の0番目に0を挿入する
ブロックの文言は公式の日本語版に合わせています(形は簡易表示です)。
[0].insertAt(0, 0)[0].insert_at(0, 0)引数(ひきすう)
引数とは、ブロックの中で自分で選んだり入力したりする部分のことです。
| 名前 | 選べる値 | 意味 |
|---|---|---|
| index | 数値 | 値を挿入するリスト中の位置を 0 から始まる値で指定します。たとえば:0 |
| value | T |
使用例
The size of the array grows by one. The element is added to the array at the position you want. If there's an element already at that position, then it and all the other elements after it are moved (shifted) to the next higher position. So, an array that has the numbers 4, 5, 9, 3 will be 4, 5, 9, 2, 3 if 2 is inserted at position 3. It looks like this in blocks:
JavaScript
let myNumbers = [4, 5, 9, 3];
myNumbers.insertAt(3, 2);Python
myNumbers = [4, 5, 9, 3]
myNumbers.insert_at(3, 2)Make a ordered array that has the numbers from a jumbled an array in order of lowest to highest.
JavaScript
let removed = 0
let jumbled = [4, 5, 2, 1, 6, 9, 0, 3, 8, 7]
let ordered = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for (let item of jumbled) {
ordered.insertAt(item, item)
removed = ordered.removeAt(item + 1)
}Python
removed = 0
jumbled = [4, 5, 2, 1, 6, 9, 0, 3, 8, 7]
ordered = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for item in jumbled:
ordered.insert_at(item, item)
removed = ordered.remove_at(item + 1)