Skip to content

Instantly share code, notes, and snippets.

@mrgarita
Last active April 1, 2020 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrgarita/5841f495c4343b01489b6f7c86457924 to your computer and use it in GitHub Desktop.
Save mrgarita/5841f495c4343b01489b6f7c86457924 to your computer and use it in GitHub Desktop.
リスト内の検索を使ったデータ更新
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace リスト内の検索を使ったデータ更新
{
class Program
{
static void Main(string[] args)
{
// リストデータを定義
List<string> list = new List<string>();
// データを追加
list.Add("香川");
list.Add("愛媛");
list.Add("北海道");
list.Add("高知");
// 更新前のデータを表示
foreach (string x in list)
{
Console.WriteLine(x);
}
// データを更新
int index = list.IndexOf("北海道"); // 「北海道」の添え字番号を取得
list.RemoveAt(index); // 削除(1)
list.Insert(index, "徳島"); // 削除した位置に挿入(2)
// (1)(2)の手順の代わりに
// list[index] = "徳島"; でも同じ
// 更新後のデータを表示
Console.WriteLine("\n-----------更新後-------------");
foreach (string x in list)
{
Console.WriteLine(x);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment