List<List<int>>でこのような構造を作りました。
C#
1List<List<int>> nestedList = new List<List<int>> 2 { 3 new List<int>{ 4 1,1,3 5 }, 6 new List<int> 7 { 8 2,0,7 9 }, 10 new List<int> 11 { 12 5,1,4 13 }, 14 new List<int> 15 { 16 9,1,8 17 }, 18 new List<int> 19 { 20 15,0,9 21 } 22 };
列1 | 列2 | 列3 |
---|---|---|
1 | 1 | 3 |
2 | 0 | 7 |
5 | 1 | 4 |
9 | 1 | 8 |
15 | 0 | 9 |
このとき、列1(1,2,5,9,15)から任意の数を検索して、当てはまる行の2,3列目の数を取得する方法が知りたいです。
例えば、列1に「9」を含む行の2列目の数は「1」、2列目の数は「8」というように取得したいです。
C#
1 int SearchFirst = 9; 2 int ResultSecond = 0; 3 int ResultThird = 0; 4 foreach (var list in nestedList) 5 { 6 if (list[0] == SearchFirst) 7 { 8 ResultSecond = list[1]; 9 ResultThird = list[2]; 10 } 11 } 12 Console.WriteLine($@"SearchFirst = {SearchFirst},ResultSecond = {ResultSecond},ResultThird = {ResultThird}");
のようにforeachで取得もできるのですが、検索が頻繁に行われるためList<int>の数が多くなるとパフォーマンスが心配です。
他に効率の良い検索方法や、そもそもListの入れ子じゃなくてこうしたら検索しやすい、などありましたら教えてください。
宜しくお願いします。


回答1件
あなたの回答
tips
プレビュー