陣列
該文件涵蓋了基本的陣列操作,包括創建、讀取、修改和各種操作,例如查找位置、反轉以及在特定位置插入和刪除值。
創造
設置數位數位數位數位陣組
將清單設置為陣列
{"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>"}
輸出:
設置文字陣列
將文字清單設置為陣列
{"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>"}
輸出:
創建新陣列
空陣列
{"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>"}
輸出:
顯示和刪除最後一項
從清單中獲取和刪除最後一個值
{"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>"}
輸出:
刪除最後一個值
從清單中刪除最後一個值
{"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>"}
輸出:
刪除第一個值
從清單中刪除第一個值
{"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>"}
輸出:
在特定位置刪除值
列出刪除值
{"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>"}
輸出:
操作
查找值的位置
清單查找索引
{"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>"}
輸出:
目録