简体   繁体   中英

Get element in array in arraylist

I'm trying to be concise. I couldn't find answers online, though I'm sure they're there, but I reviewed these questions on SO and didn't find what I was looking for:

compare-and-retrieve-elements-from-arraylist

getting-index-of-an-item-in-an-arraylist

getting-a-particular-arraylist-element

I have an ArrayList with a number of arrays in them, each of which are one-dimensional and have varying amounts of elements (both string and int). How can I access the elements in the ArrayList ?

Linq has an extension method SelectMany that will make it look like all the child lists are one list

array.SelectMany( i => i );

You can use OfType and SelectMany ,for example you can get all string and integer values inside of one-dimensional arrays like this:

ArrayList arr = new ArrayList();
arr.Add(new []{ 1, 2, 3, 4, 5});
arr.Add(new [] {"asdas", "asdsa"});
var stringValues = arr.OfType<Array>()
                      .SelectMany(x => x.OfType<string>());  // asdas asdsa
var intValues = arr.OfType<Array>()
                   .SelectMany(x => x.OfType<string>());  // 1 2 3 4 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM