ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2024ǯ06·î24Æü(·î) 10:05:08ÍúÎò
¡¡Find ¥á¥½¥Ã¥É¤Ï List ¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë¥á¥½¥Ã¥É¤Î£±¤Ä¤Ç¤¹¡£
List Æâ¤òÀèƬ¤«¤é¾ò·ï¤Ë¹ç¤ï¤»¤Æ¸¡º÷¤·¡¢ºÇ½é¤Ë¾ò·ï¤Ë¹çÃפ·¤¿¾ðÊó¤ò£±¤Ä¤À¤±Ãê½Ð¤·¤Þ¤¹¡£
»²¹Í¥µ¥¤¥È
MicroSoft
List.Find ¥á¥½¥Ã¥É
Ararami Studio ÍÍ
C# ¤ÎÇÛÎó¤äList¤ò¸¡º÷¤¹¤ë (Find,FindAll,FindIndex)
°é»ù¥Ñ¥Ñ¤Î¿ÍÀ¸È÷˺Ͽ ÍÍ
¡ÚC#¡Û¥ê¥¹¥È·¿¤È¥é¥à¥À¼°(Find/FindAll)
¡¡Find¥á¥½¥Ã¥É¤Ï List ÆÃÄê¤Î¾ò·ï¤òËþ¤¿¤¹ºÇ½é¤ÎÍ×ÁǤò¸«¤Ä¤±¤ë¤È¤¤¤¦¡¢¤è¤¯»È¤ï¤ì¤ëÁàºî¤òÄ󶡤·¤Þ¤¹¡£
¤³¤ì¤Ï Find ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢foreach ʸ¤È if ʸ¤òÍøÍѤ·¤¿¥±¡¼¥¹¤Ç¼ÂÁõ¤µ¤ì¤Þ¤¹¡£
¡¡°Ê²¼¤ËFind¥á¥½¥Ã¥É¤òÍøÍѤ»¤º¤Ëforeach ʸ¤È if ʸ¤òÍøÍѤ·¤¿¥±¡¼¥¹¡¢Find ¥á¥½¥Ã¥É¤òÍøÍѤ·¤¿¥±¡¼¥¹¡¢¤½¤ì¤ËÂбþ¤¹¤ëLINQ¤ÎFirstOrDefault¥á¥½¥Ã¥É¤Î»ÈÍÑÎã¤ò¼¨¤·¤Þ¤¹¡£
¡¡¤³¤³¤Ç¤Ï¡¢¥ê¥¹¥ÈÆâ¤Î¿ôÃͤÇ10¤è¤êÂ礤¤ºÇ½é¤ÎÍ×ÁǤò¸«¤Ä¤±¤ë½èÍý¤ò¹Í¤¨¤Þ¤¹¡£
¡¡List Æâ¤ÎÁàºî¤ò¹Ô¤¦¾ì¹ç¡¢foreach ʸ¤È if ʸ¤Ë¤è¤ëÁȤ߹ç¤ï¤»¤Ë¤è¤Ã¤Æ¡¢¾ðÊó¤òÀººº¤·¡¢É¬ÍפÊÍ×ÁǤò¸«¤Ä¤±¤Þ¤¹¡£
List<int> numbers = new List<int> { 5, 7, 12, 20, 25 }; int firstNumberGreaterThanTen = 0; foreach (int number in numbers) { if (number > 10) { firstNumberGreaterThanTen = number; break; } }
¡¡¾åµ¤ÎÎã¤òƧ¤Þ¤¨¤Æ¡¢Æ±¤¸½èÍý¤ò Find ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ¼ÂÁõ¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¡¡Find ¥á¥½¥Ã¥É¤Ï List ¥¯¥é¥¹¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë¥á¥½¥Ã¥É¤Î£±¤Ä¤Ç¤¹¡£
List<T>.Find(Predicate<T>) ¥á¥½¥Ã¥É
https://learn.microsoft.com/ja-jp/dotnet/api/syste...
List<int> numbers = new List<int> { 5, 7, 12, 20, 25 }; int firstNumberGreaterThanTen = numbers.Find(number => number > 10);
¡¡Èó¾ï¤Ë¥·¥ó¥×¥ë¤Ê½èÍý¤Ë¤Ê¤ê¤Þ¤·¤¿¡£
¡¡foreach Æâ¤Ëµ½Ò¤·¤Æ¤¤¤¿ if ʸ¤Î¾ò·ïÉôʬ¤¬¥é¥à¥À¼°¤Î±¦ÊÕ¤ËÅö¤¿¤ëÉôʬ¤ËÍøÍѤµ¤ì¤Æ¤¤¤Þ¤¹¡£
¡¡¤½¤Î¤¿¤á¡¢Find ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ½èÍý¤òÃÖ¤´¹¤¨¤ë¾ì¹ç¤Ë¤Ï¡¢½¾Íè¤Îµ½ÒÊýË¡¤ò¤·¤Ã¤«¤ê¤ÈÍý²ò¤·¤Æ¤ª¤¯¤³¤È¤¬ÂçÀڤǤ¹¡£
¡¡¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤òÄ̤¸¤Æ¡¢´û¸¤Î½èÍý¤òÃÖ¤´¹¤¨¤Ê¤¬¤é³Ð¤¨¤Æ¤¤¤¯¤è¤¦¤Ë¤¹¤ë¤È¤è¤¤¤Ç¤·¤ç¤¦¡£
¡¡¤Ê¤ª¡¢Find ¥á¥½¥Ã¥É¤Ç³ºÅö¤¹¤ëÍ×ÁǤ¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¡¢·¿¤Î¥Ç¥Õ¥©¥ë¥È(µ¬Äê)ÃÍ(»²¾È·¿¤Ï null¡£int ·¿¤Ê¤é 0¡Ë¤òÌᤷ¤Þ¤¹¡£
¡¡¤³¤Á¤é¤Î¥á¥½¥Ã¥É¤Ï LINQ ¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë Find ¥á¥½¥Ã¥É¤òƱÍͤε¡Ç½¤ò»ý¤Ä¥á¥½¥Ã¥É¤Ç¤¹¡£
using System.Linq; ¤òµ½Ò¤·¤ÆÍøÍѤ·¤Þ¤¹¡£
Enumerable.First ¥á¥½¥Ã¥É
https://learn.microsoft.com/ja-jp/dotnet/api/syste...
Enumerable.FirstOrDefault ¥á¥½¥Ã¥É
https://learn.microsoft.com/ja-jp/dotnet/api/syste...
¡¡FirstOrDefalt ¥á¥½¥Ã¥É¤Ï Find ¥á¥½¥Ã¥É¤ÈƱ¤¸¤è¤¦¤ËÆ°ºî¤·¤Þ¤¹¤Î¤Ç¡¢³ºÅö¤¹¤ëÍ×ÁǤ¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¡¢·¿¤Î¥Ç¥Õ¥©¥ë¥È(µ¬Äê)ÃÍ(»²¾È·¿¤Ï null¡Ë¤òÌᤷ¤Þ¤¹¡£
Find ¥á¥½¥Ã¥É¤Î°ã¤¤¤È¤·¤Æ¤Ï¡¢ÃÙ±ä¼Â¹Ô½èÍý¤µ¤ì¤Þ¤¹¤Î¤Ç¡¢Î㤨¤Ð¡¢Where ¥á¥½¥Ã¥É¤ÈÁȤ߹ç¤ï¤»¤ÆÍøÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡¤¿¤À¤·¡¢First ¥á¥½¥Ã¥É¤Î¾ì¹ç¤Ë¤Ï¡¢Argument Null Exception ¥¨¥é¡¼¤È¤Ê¤ê¤Þ¤¹¤Î¤ÇÃí°Õ¤·¤Æ¤¯¤À¤µ¤¤¡£
´ðËÜŪ¤Ë¤Ï FirstOrDefalt ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Þ¤·¤ç¤¦¡£
List<int> numbers = new List<int> { 5, 7, 12, 20, 25 }; int firstNumberGreaterThanTen = numbers.FirstOrDefault(number => number > 10);
¡¡¤³¤ì¤é¤Î¥³¡¼¥É¤Ï¤¹¤Ù¤Æ¡¢Æ±¤¸·ë²Ì¡Ê12¡Ë¤òÊÖ¤·¤Þ¤¹¡£
¡¡Character ¤È Item ¥¯¥é¥¹¤ò»È¤Ã¤ÆÎã¤ò¼¨¤·¤Þ¤¹¡£
¡¡¤Þ¤º¡¢¥²¡¼¥àÆâ¤Ç¥¥ã¥é¥¯¥¿¡¼¤Î¥ê¥¹¥È¤ò´ÉÍý¤·¡¢¤½¤ì¤é¤Î¥¥ã¥é¥¯¥¿¡¼¤¬»ý¤Ã¤Æ¤¤¤ë¥¢¥¤¥Æ¥à¤Î¥ê¥¹¥È¤ò°·¤¦¥·¥Á¥å¥¨¡¼¥·¥ç¥ó¤ò¹Í¤¨¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¡¡°Ê²¼¤Î¤è¤¦¤Ê¥¯¥é¥¹¹½Â¤¤òÁÛÄꤷ¤Þ¤¹¡£
public class Item { public string Name { get; set; } public int Value { get; set; } } public class Character { public string Name { get; set; } public List<Item> Items { get; set; } }
¡¡¤³¤³¤Ç¤Ï¡¢Character¤Î¥ê¥¹¥È¤«¤é¡¢Ì¾Á°¤¬ÆÃÄê¤Îʸ»úÎó¤Ë°ìÃפ¹¤ëºÇ½é¤ÎCharacter¤òõ¤¹½èÍý¤ò¹Í¤¨¤Þ¤¹¡£
¡¡¤Þ¤º¤Ï½é´ü²½¤ò¹Ô¤¤¡¢List ¤Î½àÈ÷¤òÀ°¤¨¤Þ¤¹¡£
// ¥¢¥¤¥Æ¥à¤ÎºîÀ® Item sword = new Item { Name = "Sword", Value = 100 }; Item shield = new Item { Name = "Shield", Value = 150 }; Item potion = new Item { Name = "Potion", Value = 50 }; // ¥¥ã¥é¥¯¥¿¡¼¤ÎºîÀ®¤È¥¢¥¤¥Æ¥à¤Î³ä¤êÅö¤Æ Character john = new Character { Name = "John", Items = new List<Item> { sword, shield } }; Character mary = new Character { Name = "Mary", Items = new List<Item> { potion } }; Character tom = new Character { Name = "Tom", Items = new List<Item> { sword, potion } }; // ¥¥ã¥é¥¯¥¿¡¼¤ò¥ê¥¹¥È¤Ë³ÊǼ List<Character> characters = new List<Character> { john, mary, tom };
¡¡¤³¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤Ï¡¢3¤Ä¤Î¥¥ã¥é¥¯¥¿¡¼¡ÊJohn, Mary, Tom¡Ë¤òºîÀ®¤·¡¢¤½¤ì¤¾¤ì¤Ë°Û¤Ê¤ë¥¢¥¤¥Æ¥à¤ò³ä¤êÅö¤Æ¤Æ¤¤¤Þ¤¹¡£
¤½¤Î¸å¡¢ºîÀ®¤·¤¿¥¥ã¥é¥¯¥¿¡¼¤òcharacters¤È¤¤¤¦Ì¾Á°¤Î¥ê¥¹¥È¤Ë³ÊǼ¤·¤Þ¤¹¡£
¡¡¤³¤ì¤Ç¡¢Find ¥á¥½¥Ã¥É¤ò»È¤Ã¤ÆÆÃÄê¤Î¥¥ã¥é¥¯¥¿¡¼¤ä¤½¤Î½ê»ý¥¢¥¤¥Æ¥à¤ò¸¡º÷¤¹¤ë½àÈ÷¤¬¤Ç¤¤Þ¤·¤¿¡£
¡¡Ãê½Ð¤·¤¿Character¤Î¾ðÊó¤òÂåÆþ¤¹¤ë¤¿¤á¤ÎÊÑ¿ô¤òÍÑ°Õ¤·¡¢¤½¤ì¤«¤é foreach ʸ¤òÍøÍѤ·¤Æ List Æâ¤Î¾ðÊó¤ò£±¤Ä¤º¤Ä¼è¤ê½Ð¤·¡¢
if ʸ¤ÇȽÄê¤ò¹Ô¤¤¡¢¾ò·ï¤Ë¹çÃפ¹¤ë Character ¤ò¸«¤Ä¤±¤ë½èÍý¤Ç¤¹¡£
Character foundCharacter = null; foreach (Character character in characters) { if (character.Name == "John") { foundCharacter = character; break; } }
¡¡¤³¤Î½èÍý¤Îµ¡Ç½¤òÍý²ò¤·¤¿¾å¤Ç¡¢Find ¥á¥½¥Ã¥É¤Ø¤ÎÃÖ¤´¹¤¨¤ò¤ª¤³¤Ê¤Ã¤Æ¤¤¤¤Þ¤¹¡£
¡¡¾åµ¤ÎÎã¤òƧ¤Þ¤¨¤Æ¡¢Æ±¤¸½èÍý¤ò Find ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ¼ÂÁõ¤·¤Þ¤¹¡£
Character foundCharacter = characters.Find(character => character.Name == "John");
¡¡Àè¤Û¤É¤ÈƱ¤¸¤Ç¡¢¤³¤Á¤é¤âÈó¾ï¤Ë¥·¥ó¥×¥ë¤Ê½èÍý¤Ë¤Ê¤ê¤Þ¤·¤¿¡£
ÊÑ¿ô¤ÎÀë¸À¤ÈƱ»þ¤Ë¡¢Find ¥á¥½¥Ã¥É¤òÍøÍѤ¹¤ë¤³¤È¤Ç¡¢¸«¤Ä¤±¤¿ Character ¤Î¾ðÊó¤ò Find ¥á¥½¥Ã¥É¤Î¼Â¹Ô·ë²Ì¤ÎÌá¤êÃͤȤ·¤Æ¼õ¤±¼è¤ë½èÍý¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
Character foundCharacter = characters.FirstOrDefault(character => character.Name == "John");¡¡
¡¡¤³¤ì¤é¤Î¥³¡¼¥É¤Ï¤É¤Á¤é¤â¡¢Ì¾Á°¤¬ "John" ¤ÎºÇ½é¤ÎCharacter¤òÊÖ¤·¤Þ¤¹¡£
¡¡¤³¤Î¤è¤¦¤Ë¡¢foreach ʸ¤È if ʸ¤òÍøÍѤ·¤Æ List Æ⤫¤éɬÍפÊÍ×ÁǤò¸«¤Ä¤±¤ë½èÍý¤òµ½Ò¤¹¤ëºÝ¤Ë¤Ï
Find¥á¥½¥Ã¥É¤äFirstOrDefault¥á¥½¥Ã¥É¤ò»È¤¦¤³¤È¤Ç¡¢¾ò·ï¤Ë°ìÃפ¹¤ëºÇ½é¤ÎÍ×ÁǤòõ¤¹½èÍý¤òÈó¾ï¤Ë¥·¥ó¥×¥ë¤Ë½ñ¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡List.Find¥á¥½¥Ã¥É¤Ï¼ÂºÝ¤Ë¤ÏLINQ¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¤¬¡¢¥é¥à¥À¼°¤ò»ÈÍѤ·¤Æ¥Ç¡¼¥¿¤òÁàºî¤¹¤ë¤È¤¤¤¦ÅÀ¤ÇLINQ¤È¶¦Ä̤·¤Æ¤¤¤Þ¤¹¡£
¤è¤Ã¤Æ¡¢Find ¥á¥½¥Ã¥É¤òÍý²ò¤¹¤ë¤³¤È¤Ï¡¢LINQ¤ÎÍý²ò¤ò¿¼¤á¤ë¾å¤ÇÌòΩ¤Á¤Þ¤¹¡£
¡¡LINQ ¤Ï List ¤ÎÁàºî¤ò¹Ô¤¤¤ä¤¹¤¯¤¹¤ë¤¿¤á¤Îµ¡Ç½¤Ç¤¹¡£
¤½¤Î¤¿¤á´ðËÜŪ¤Ë¤Ï¡¢foreach ʸ¤È if ʸ¤òÁȤ߹ç¤ï¤»¤ë¤³¤È¤ÇƱÍͤνèÍý¤òºîÀ®¤Ç¤¤Þ¤¹¤¬¡¢
¤³¤ÎÁàºî¤ò¤è¤ê¥·¥ó¥×¥ë¤Ë¡¢¤Þ¤¿¸úΨŪ¤Ë¹Ô¤¦¤¿¤á¤Î¥Ä¡¼¥ë¤È¤·¤ÆÀ߷פµ¤ì¤Æ¤¤¤Þ¤¹¡£
¡¡½¾Íè¤Îforeach¥ë¡¼¥×¤Ë¤è¤ëÊýË¡¤Ï¡¢Íý²ò¤·¤ä¤¹¤¯¡¢ÌÀ¼¨Åª¤ÊÁàºî¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¤¬¡¢¥³¡¼¥É¤¬¾éĹ¤Ë¤Ê¤ë¤³¤È¤¬¤¢¤ê¤Þ¤¹
Æäˡ¢Ê£»¨¤Ê½èÍý¤ò¹Ô¤¦¾ì¹ç¤ä¡¢Ê£¿ô¤ÎÁàºî¤ò°ìÅ٤˹Ԥ¦¾ì¹ç¤Ë¤Ï¡¢foreach¥ë¡¼¥×¤ò¥Í¥¹¥È¤·¤¿¤ê¡¢Ê£¿ô¤Îifʸ¤òÁȤ߹ç¤ï¤»¤¿¤ê¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
¡¡¤½¤ì¤ËÂФ·¤Æ¡¢LINQ¤Ï¡¢°ìÏ¢¤ÎÁàºî¤ò°ì¹Ô¤Çµ½Ò¤¹¤ë¤³¤È¤¬¤Ç¤¡¢¥³¡¼¥É¤Î¸«Ä̤·¤¬Îɤ¯¤Ê¤ë¤³¤È¤¬¤¢¤ê¤Þ¤¹¡£
¤Þ¤¿¡¢LINQ¤ÏÃÙ±ä¼Â¹Ô¤¬²Äǽ¤Ê¤Î¤Ç¡¢A ¤Î½èÍý·ë²Ì¤ò»È¤Ã¤Æ B ¤ÎÃͤò¸«¤Ä¤±¤ë¡¢¤È¤¤¤Ã¤¿¤è¤¦¤Ë¡¢1²ó¤Îµ½Ò¤ÇɬÍפʥǡ¼¥¿¤À¤±¤ò¸úΨ¤è¤¯½èÍý¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡¤¿¤À¤·¡¢¥Ñ¥Õ¥©¡¼¥Þ¥ó¥¹¤Ëα°Õ¤¹¤ëÅÀ¤ä¡¢³Ø½¬¤Î¤¿¤á¤Ë»öÁ°¤Ë³Ð¤¨¤ë¤³¤È¤¬Â¿¤¤¤¿¤á¡¢½é³Ø¼Ô¤Ë¤È¤Ã¤Æ¤ÏÍý²ò¤¹¤ë¤Î¤¬Æñ¤·¤¤Éôʬ¤Ç¤â¤¢¤ê¤Þ¤¹¡£
¡¡¤Þ¤º¤Ï¤·¤Ã¤«¤ê¤È¡¢List ¤ÎÁàºî¤ò¹Ô¤¨¤ë¤è¤¦¤Ë¤·¡¢¤½¤Î¸å¡¢LINQ ¤ò°·¤¦¤¿¤á¤ËɬÍפʳؽ¬(¥Ç¥ê¥²¡¼¥È¡¢¥é¥à¥À¼°¡¢ÃÙ±ä½èÍý¡¢¥¸¥§¥Í¥ê¥Ã¥¯¤Ê¤É)¤ò³Ø½¬¤·¤Æ¤¤¤¯¤È¤è¤¤¤Ç¤·¤ç¤¦¡£
¡¡=>¡¡´û¸¤Î½èÍý¤ò¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤·¤Æ³Ø¤Ö LINQ ³èÍÑÎã¡
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯