0

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?

Community
  • 1
  • 1
  • Please remove the arraylist tag if it doesn't apply. While C# does have a class under System.Collections called ArrayList, the tag explicitly cites Java, so I'm not sure if I should use it. –  Feb 18 '14 at 23:50
  • What type of elements do you want to access ? all elements inside of one-dimensional arrays ? – Selman Genç Feb 18 '14 at 23:51
  • The ArrayList contains lists of arrays. Those arrays have varying numbers of items in them. The items in any of those arrays can be either strings or integers, and may appear in different configurations. –  Apr 06 '14 at 01:54

2 Answers2

0

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

array.SelectMany( i => i );

0

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
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • I can't find the methods OfType or SelectMany. I am not familiar with Linq, but it looks like (according to the answer below) that might be what you are talking about. –  Feb 19 '14 at 00:01
  • @Stopforgettingmyaccounts... what version of .NET Framework you are using? add a reference to System.Linq.dll, `using System.Linq` – Selman Genç Feb 19 '14 at 00:02
  • The *using* statement works and I am using version 4.0. I didn't know any references added methods to other references. The Linq import added OfType(), but it did not add SelectMany(). –  Feb 19 '14 at 00:04
  • it is not _possible_, `SelectMany` is supported in 4.0 see [documentation](http://msdn.microsoft.com/en-us/library/bb534336(v=vs.110).aspx), and also SelectMany and OfType are in the same assembly.You are doing something wrong. – Selman Genç Feb 19 '14 at 00:07
  • @Stopforgettingmyaccounts... SelectMany method doesn't take ArrayList as first parameter.You should use OfType or Cast methods to access that method because it takes `IEnumerable`.See my answer, did I use Arraylist.SelectMany ? – Selman Genç Feb 19 '14 at 00:14
  • You see `OfType` because it takes `IEnumerable` as first parameter not `IEnumerable`, and `ArrayList` implements `IEnumerable`, not `IEnumerable` because it is not **generic** – Selman Genç Feb 19 '14 at 00:16
  • @Stopforgettingmyaccounts... why don't you just try this answer and see it is correct or wrong? – Selman Genç Feb 19 '14 at 00:21
  • Copying it doesn't teach me it and I can't use it effectively if the first thing i try to do on my own fails. I'm going to try and figure out what's going on there. –  Feb 19 '14 at 00:23
  • Okay, now that I known about extension methods (http://msdn.microsoft.com/en-us/library/bb383977.aspx), I know where SelectMany() came from. I learned that the IEnumerable interface just deals with overloading the indexing operator []. (As you can see, I am still learning C#.) I now understand this code; thanks. It works for what I wanted. –  Apr 06 '14 at 02:03