CRS - 配列
配列の要素を削除したい
配列を扱うクラスでは、それぞれ以下のメソッドを利用して要素の削除を行います。
Arrayクラス
eraseメソッド
指定位置の要素または全ての要素を削除します。
popメソッド
最後の要素を取り除き、その要素を返します。
Mapクラス
eraseメソッド
指定位置の要素または全ての要素を削除します。
1.Arrayクラス
Form Array { x = 0; y = 0; width = 400; height = 597; # 配列 Array arr1; arr1[0] = "ABC"; arr1[1] = "あいう"; arr1[2] = "123"; Label label1 { x = 36; y = 33; width = 322; height = 31; value = "配列の内容(デフォルト):ABC/あいう/123"; horizontalAlign = ALIGN_CENTER; verticalAlign = ALIGN_MIDDLE; bgColor = Color.WHITE; border = true; } EditBox editbox1 { x = 38; y = 216; width = 326; height = 157; } Button button1 { x = 152; y = 82; width = 204; height = 35; title = "要素を指定して削除(index:1)"; function onTouch(e) { # 要素番号が1の要素を削除 ^.arr1.erase(1); # 削除後の要素数、value値をeditboxに表示 ^.setEditbox(); } } Button button2 { x = 152; y = 128; width = 204; height = 35; title = "全要素を削除"; function onTouch(e) { # 配列の全要素を削除 ^.arr1.erase(); # 削除後の要素数、value値をeditboxに表示 ^.setEditbox(); } } Button button3 { x = 152; y = 176; width = 204; height = 35; title = "末尾の要素を削除"; function onTouch(e) { # 末尾の要素を削除 ^.arr1.pop(); # 削除後の要素数、value値をeditboxに表示 ^.setEditbox(); } } Button button4 { x = 136; y = 408; width = 228; height = 35; title = "初期化"; function onTouch(e) { # arrayの初期化 ^.arr1.erase(); ^.arr1[0] = "ABC"; ^.arr1[1] = "あいう"; ^.arr1[2] = "123"; # editboxの初期化 ^.setEditbox(); } } Function setEditbox(){ editbox1.value = ""; var i; for (i in arr1) { # 取得した各要素のvalue値をEditBoxに表示 editbox1 = editbox1 + str(i) + "番目:" + arr1[i] + "\r\n"; } # 配列の要素数をEditBoxに表示 editbox1.value = editbox1 + "要素数:" + str(arr1.length); } if (!Application.DESIGNTIME) { # editbox表示設定 this.setEditbox(); } }
2.Mapクラス
Form Map { x = 0; y = 0; width = 400; height = 597; # 配列 Map mp; mp["data1"] = "データ1"; mp["data2"] = "データ2"; mp["data3"] = "データ3"; Label label1 { x = 28; y = 33; width = 322; height = 31; value = "配列の内容(デフォルト):データ1/データ2/データ3"; horizontalAlign = ALIGN_CENTER; verticalAlign = ALIGN_MIDDLE; bgColor = Color.WHITE; border = true; } EditBox editbox1 { x = 22; y = 183; width = 326; height = 157; } Button button1 { x = 144; y = 82; width = 204; height = 35; title = "要素(data2)を指定して削除"; function onTouch(e) { # "data2"の要素を削除 ^.mp.erase("data2"); # 削除後の要素数、value値をeditboxに表示 ^.setEditbox(); } } Button button2 { x = 144; y = 128; width = 204; height = 35; title = "全要素を削除"; function onTouch(e) { # 配列の全要素を削除 ^.mp.erase(); # 削除後の要素数、value値をeditboxに表示 ^.setEditbox(); } } Button button3 { x = 120; y = 352; width = 228; height = 35; title = "初期化"; function onTouch(e) { # mapの初期化 ^.mp.erase(); ^.mp["data1"] = "データ1"; ^.mp["data2"] = "データ2"; ^.mp["data3"] = "データ3"; # editboxの初期化 ^.setEditbox(); } } Function setEditbox(){ editbox1.value = ""; var i; # 取得した各要素の値をEditBoxに表示 # ※for-in文で取り出される順序は不定です for (i in mp) { editbox1 += "添え字(キー):" +str(i) +" 値:" + mp[i] + "\r\n"; } # 配列の要素数をEditBoxに表示 editbox1.value = editbox1 + "要素数:" + str(mp.length); } if (!Application.DESIGNTIME) { # editbox表示設定 this.setEditbox(); } }
Biz-Collections Bizの宝箱 トップへ
Biz/Browser DT・Biz/Designer DT TIPS集 トップへ