Get array items by index

  • A+
Category:Languages

I have two arrays, one with values an one with indices

int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 }; int[] indices = { 1, 3, 5, 6, 7, 9 }; 

now I want a result array from the items selected by the indices of indices array

// 2, 7, 9, 13, 19 int[] result = new []{ items[1], items[3], items[5], items[6], items[7], items[9] };  

Question: Is there a more generic approach for this?


var results = Array.ConvertAll(indices, i => items[i]); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: