> 

 > 

5. 以積木式程式製作Raspberry Pi智慧裝置
阵列
阵列

该文件涵盖了基本的阵列操作,包括创建、读取、修改和各种操作,例如查找位置、反转以及在特定位置插入和删除值。

创造

设置数位数位数位数位阵组

将清单设置为阵列

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable></variables><block type=\"variables_set\" disabled=\"true\" x=\"-23\" y=\"68\"><field name=\"VAR\">list</field><value name=\"VALUE\"><block type=\"lists_create_with\" disabled=\"true\"><mutation items=\"2\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"ADD1\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">1</field></shadow></value></block></value></block></xml>"}

此块用于将数位阵列设置为变数。

参数: 数字(预设值:0, 1)

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>value</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"2\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"ADD1\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><next><block type=\"pxt_controls_for_of\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">value</field></shadow></value><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><statement name=\"DO\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">value</field></block></value></block></value></block></statement></block></next></block></statement></block></xml>"}

输出:

将清单设置为 Array of - 输出

设置文字阵列

将文字清单设置为阵列

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>value</variable><variable>text list</variable></variables><block type=\"variables_set\" disabled=\"true\" x=\"70\" y=\"284\"><field name=\"VAR\">text list</field><value name=\"VALUE\"><block type=\"lists_create_with\" disabled=\"true\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">c</field></shadow></value></block></value></block></xml>"}

此块用于将文字阵列设置为变数。

参数: 字串(预设值:a、b、c)

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></statement></block></xml>"}

输出:

将文本清单设置为 Array of - 输出

创建新阵列

空阵列

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable></variables><block type=\"lists_create_with\" disabled=\"true\" x=\"-22\" y=\"292\"><mutation items=\"0\" horizontalafter=\"3\"/></block></xml>"}

此块用于创建新阵列。

参数: 屏幕专案

输出: 阵列

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>array</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"22\" y=\"383\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">array</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"2\" horizontalafter=\"3\"/><value name=\"ADD0\"><block type=\"text\"><field name=\"TEXT\">text 1</field></block></value><value name=\"ADD1\"><block type=\"text\"><field name=\"TEXT\">text 2</field></block></value></block></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">array</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">array</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></statement></block></xml>"}

输出:

空阵列 - 输出

计算数位的长度

阵列清单的长度

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>array</variable><variable>index</variable><variable>list</variable></variables><block type=\"lists_length\" disabled=\"true\" x=\"112\" y=\"293\"><value name=\"VALUE\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

此块用于显示阵列中的项目数。

参数: 萤幕项目(阵列)

输出:

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value></block></value></block></next></block></statement></block></xml>"}

输出:

阵列清单的长度 - 输出

显示带有所选索引的值

清单获取值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>list</variable></variables><block type=\"lists_index_get\" disabled=\"true\" x=\"-22\" y=\"472\"><value name=\"LIST\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

此块用于显示阵列中给定索引处的值。

参数:

  • 萤幕项目(阵列)

输出: 屏幕专案

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></value></block></next></block></statement></block></xml>"}

输出:

清单获取值 - 输出

显示将被删除的值

列出 get 和 remove 值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>list</variable></variables><block type=\"array_removeat\" disabled=\"true\" x=\"-22\" y=\"338\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value><value name=\"index\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

这个块用于显示和删除特定索引处的值。

参数:

  • 萤幕项目(阵列)

输出: 屏幕专案

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"array_removeat\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"index\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value></block></next></block></statement></block></xml>"}

输出:

列出 Get 和 Remove at 的值 - 输出

显示和删除最后一项

从清单中获取和删除最后一个值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>list</variable></variables><block type=\"array_pop\" disabled=\"true\" x=\"-22\" y=\"383\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

这个块用于显示和删除阵列内的最后一个值。

参数: 萤幕项目(阵列)

输出: 屏幕专案

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"array_removeat\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"index\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value></block></next></block></statement></block></xml>"}

输出:

从 - 输出中获取和删除最后一个值

显示和删除第一项

从清单中获取和删除第一个值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>list</variable></variables><block type=\"array_shift\" disabled=\"true\" x=\"-22\" y=\"338\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

此块用于显示和删除阵列内的第一个值。

参数: 萤幕项目(阵列)

输出: 屏幕专案

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"array_shift\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value></block></value><next><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"After removing th e value, the remaining values are:\"</field></block></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

从清单中获取和删除第一个值 - 输出

显示随机值

从清单中获取随机值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable><variable>list</variable></variables><block type=\"array_pickRandom\" disabled=\"true\" x=\"-22\" y=\"652\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

修改

修改数位内部的值

列出 0 到 ? 处的设置值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable><variable>list</variable></variables><block type=\"lists_index_set\" disabled=\"true\" x=\"23\" y=\"563\"><value name=\"LIST\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

此块用于修改数位中选定位置的值。

参数: 萤幕项目(阵列)

条件:

  • 数字(预设值:0)
  • 屏幕专案

输出: N/A

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"lists_index_set\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"VALUE\"><shadow type=\"text\"><field name=\"TEXT\">d</field></shadow></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

列出设定值 - 输出

在末尾插入值

清单增加价值…结束

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable><variable>list</variable></variables><block type=\"array_push\" disabled=\"true\" x=\"23\" y=\"292\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

此块用于将阵列末尾的值相加。

参数:

  • 萤幕项目(阵列)
  • 屏幕专案

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"array_push\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"value\"><shadow type=\"text\"><field name=\"TEXT\">END</field></shadow></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

文字清单向 End 新增值 - 输出

删除最后一个值

从清单中删除最后一个值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable><variable>list</variable></variables><block type=\"array_pop_statement\" disabled=\"true\" x=\"23\" y=\"518\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

这个块用于删除阵列中的最后一个值。

参数:

  • 萤幕项目(阵列)
  • 屏幕专案

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"array_pop_statement\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

从文本清单中删除最后一个值 -Output

删除第一个值

从清单中删除第一个值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable><variable>list</variable></variables><block type=\"array_shift_statement\" disabled=\"true\" x=\"23\" y=\"472\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

此块用于删除阵列中的第一个值。

参数:

  • 萤幕项目(阵列)
  • 屏幕专案

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text_list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"array_shift_statement\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">text_list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

从清单中删除第一个值 - 输出

在开始时增加价值

清单插入…开始时

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text_list</variable><variable>index</variable><variable>list</variable></variables><block type=\"array_unshift\" disabled=\"true\" x=\"-22\" y=\"427\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

此块用于在阵列的开头添加一个值。

参数:

  • 萤幕项目(阵列)
  • 屏幕专案

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>insert</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">insert</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"array_unshift\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"value\"><shadow type=\"text\"><field name=\"TEXT\">START</field></shadow></value></block></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

开始插入 - 输出

在开头插入值

清单插入到开头

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>insert</variable><variable>index</variable></variables><block type=\"array_unshift_statement\" disabled=\"true\" x=\"23\" y=\"517\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

此块用于在阵列的开头添加一个值。

参数:

  • 萤幕项目(阵列)
  • 屏幕专案

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"array_unshift_statement\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"value\"><shadow type=\"text\"><field name=\"TEXT\">START</field></shadow></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

开始插入 - 输出

在特定的地方增加价值

清单插入值为 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>index</variable></variables><block type=\"array_insertAt\" disabled=\"true\" x=\"23\" y=\"607\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value><value name=\"index\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

此块用于在特定位置添加一个值。

参数:

  • 萤幕项目(阵列)
  • 条件:数字(预设值:0)
  • 屏幕专案

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"array_insertAt\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"index\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value><value name=\"value\"><shadow type=\"text\"><field name=\"TEXT\">hello</field></shadow></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

清单插入到 0 值 - 输出

在特定位置删除值

列出删除值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>index</variable></variables><block type=\"array_removeat_statement\" disabled=\"true\" x=\"0\" y=\"0\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value><value name=\"index\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

这个块用于删除特定位置的值。

参数:

  • 萤幕项目(阵列)
  • 条件:数字(预设值:0)

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"array_removeat_statement\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"index\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

列出 Remove Value At - 输出

操作

查找值的位置

清单查找索引

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>index</variable></variables><block type=\"array_indexof\" disabled=\"true\" x=\"-22\" y=\"607\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

此块用于查找值的位置。

参数:

  • 萤幕项目(阵列)
  • 条件:字串/数位

输出:

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>find</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">find</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"array_indexof\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"value\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"variables_get\"><field name=\"VAR\">find</field></block></value></block></next></block></next></block></statement></block></xml>"}

输出:

清单 寻找索引 - 输出

反转阵列

反向清单

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>find</variable></variables><block type=\"array_reverse\" disabled=\"true\" x=\"23\" y=\"697\"><value name=\"list\"><block type=\"variables_get\" disabled=\"true\"><field name=\"VAR\">list</field></block></value></block></xml>"}

此块用于反转数位列中的值。

参数: 萤幕项目(阵列)

输出: N/A

范例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">list</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_create_with\"><mutation items=\"3\" horizontalafter=\"3\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">a</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">b</field></shadow></value><value name=\"ADD2\"><shadow type=\"text\"><field name=\"TEXT\">c</field></shadow></value></block></value><next><block type=\"array_reverse\"><value name=\"list\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></next></block></statement></block></xml>"}

输出:

反向清单 - 输出

目录