How to access ArrayList values inside another ArrayList?

P

Pavel Maly

Hi,
how do I access values in an ArrayList which is a part of another ArrayList? I know I can define
a class and then it is quite simple, but this is just an auxiliary application to compute some
values used later in the program. There must be a simpler way to achieve this without defining
any (even temporary) class.

Simple example:

ArrayList OuterAL = new ArrayList();
for (int i = 0; i < OuterBound; i++)
{
ArrayList InnerAL = new ArrayList();
for (int j = 0; j < InnerBound; j++)
{
InnerAL.Add(i);
}
OuterAL.Add(InnerAL);
}
// Now I need to access, let's say, the third value in the second ArrayList in OuterAL.
// How do I do it?

TIA

Pavel
 
T

Tom Spink

Pavel said:
Hi,
how do I access values in an ArrayList which is a part of another
ArrayList? I know I can define a class and then it is quite simple, but
this is just an auxiliary application to compute some values used later in
the program. There must be a simpler way to achieve this without defining
any (even temporary) class.

Simple example:

ArrayList OuterAL = new ArrayList();
for (int i = 0; i < OuterBound; i++)
{
ArrayList InnerAL = new ArrayList();
for (int j = 0; j < InnerBound; j++)
{
InnerAL.Add(i);
}
OuterAL.Add(InnerAL);
}
// Now I need to access, let's say, the third value in the second
ArrayList in OuterAL. // How do I do it?

TIA

Pavel


Hi Pavel,

Try this:

///
Console.WriteLine( ((ArrayList) OuterAL[2])[3] );
///
 
D

Dave Sexton

Hi Tom,

The third value in the second ArrayList would be:

((ArrayList) OuterAL[1])[2]

because the indexers are zero-based.

Pavel, you shouldn't declare local variables or fields in upper camel case.
Use lower camel case instead so it doesn't look like you're using static
members when you write InnerAL.Add, for example.

ArrayList outerList = new ArrayList();
for (int i =0; i < outerBound; i++)
....

And if you're using the 2.0 framework think about using List<T> instead of
ArrayList so that you won't need the cast:

List<int> intList = outerList[1][2];

--
Dave Sexton

Tom Spink said:
Pavel said:
Hi,
how do I access values in an ArrayList which is a part of another
ArrayList? I know I can define a class and then it is quite simple, but
this is just an auxiliary application to compute some values used later in
the program. There must be a simpler way to achieve this without defining
any (even temporary) class.

Simple example:

ArrayList OuterAL = new ArrayList();
for (int i = 0; i < OuterBound; i++)
{
ArrayList InnerAL = new ArrayList();
for (int j = 0; j < InnerBound; j++)
{
InnerAL.Add(i);
}
OuterAL.Add(InnerAL);
}
// Now I need to access, let's say, the third value in the second
ArrayList in OuterAL. // How do I do it?

TIA

Pavel


Hi Pavel,

Try this:

///
Console.WriteLine( ((ArrayList) OuterAL[2])[3] );
///

--
Hope this helps,
Tom Spink

Google first, ask later.
 
P

Pavel Maly

Hello Dave and Tom,
thank you both for your responses.

Dave, unfortunately, since the values are not of one type only, I can't afford to use the List.
However, I'm glad you've mentioned it, at least I know another useful thing... :) Thanks also
for the recommendation concerning lower/upper case.

With regards
Pavel


Dave Sexton napsal(a):
 
D

Dave Sexton

Hi Pavel,

Glad to help...
Dave, unfortunately, since the values are not of one type only, I can't
afford to use the List.

Actually, you still can. The point is to Type the outer list, not necessarily
the inner list. I didn't make that clear in my example, which was definitely
a bit off. Here's another:

// create a generic list of ArrayLists
List<ArrayList> outer = new List<ArrayList>();

for (int i = 0; i < outerBound; i++)
{
ArrayList inner = new ArrayList();

for (int j = 0; j < innerBound; j++)
inner.Add(i);

outer.Add(inner);
}


You can then use the following code to obtain the third value in the second
inner ArrayList:

object value = outer[1][2];


--
Dave Sexton

Pavel Maly said:
Hello Dave and Tom,
thank you both for your responses.

Dave, unfortunately, since the values are not of one type only, I can't
afford to use the List.
However, I'm glad you've mentioned it, at least I know another useful
thing... :) Thanks also
for the recommendation concerning lower/upper case.

With regards
Pavel


Dave Sexton napsal(a):
Hi Tom,

The third value in the second ArrayList would be:

((ArrayList) OuterAL[1])[2]

because the indexers are zero-based.

Pavel, you shouldn't declare local variables or fields in upper camel case.
Use lower camel case instead so it doesn't look like you're using static
members when you write InnerAL.Add, for example.

ArrayList outerList = new ArrayList();
for (int i =0; i < outerBound; i++)
...

And if you're using the 2.0 framework think about using List<T> instead of
ArrayList so that you won't need the cast:

List<int> intList = outerList[1][2];
 
P

Pavel Maly

Thanks again, Dave... Although I have coded the part I needed help with completely using
ArrayLists, the number of values I have to read from the structure forces me to rewrite it and
use the List<Type>. The code will become a lot more readable.

Have a nice day... :)

Pavel


Dave Sexton napsal(a):
Hi Pavel,

Glad to help...
Dave, unfortunately, since the values are not of one type only, I can't
afford to use the List.

Actually, you still can. The point is to Type the outer list, not necessarily
the inner list. I didn't make that clear in my example, which was definitely
a bit off. Here's another:

// create a generic list of ArrayLists
List<ArrayList> outer = new List<ArrayList>();

for (int i = 0; i < outerBound; i++)
{
ArrayList inner = new ArrayList();

for (int j = 0; j < innerBound; j++)
inner.Add(i);

outer.Add(inner);
}


You can then use the following code to obtain the third value in the second
inner ArrayList:

object value = outer[1][2];
 
T

Tom Spink

Dave said:
Hi Tom,

The third value in the second ArrayList would be:

((ArrayList) OuterAL[1])[2]

because the indexers are zero-based.

Pavel, you shouldn't declare local variables or fields in upper camel
case. Use lower camel case instead so it doesn't look like you're using
static members when you write InnerAL.Add, for example.

ArrayList outerList = new ArrayList();
for (int i =0; i < outerBound; i++)
...

And if you're using the 2.0 framework think about using List<T> instead of
ArrayList so that you won't need the cast:

List<int> intList = outerList[1][2];

Dave,

I wasn't extracting the third value in the second ArrayList, I was
extracting the fourth value in the third ArrayList.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top