どうも、ちょげ(@chogetarou)です。
リスト(List)を0埋めする方法を紹介します。
方法

リスト(List)を0埋めするには、forループを使います。
まず、for文でリストのインデックスをループします。
ループ処理で、リストのすべてのインデックスの要素に「0」を代入します。
//ls=対象の配列
for (int i = 0; i < ls.Count; i++)
{
ls[i] = 0;
}
上記のforループは、対象のリストをゼロ埋めします。
使用例
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<int> nums = new List<int>() { 1, 2, 3, 4, 5 };
for (int i = 0; i < nums.Count; i++)
{
nums[i] = 0;
}
foreach (int item in nums)
{
Console.WriteLine(item);
}
}
}
出力:
0
0
0
0
0
コメント