468,253 Members | 1,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

How to access ArrayList values inside another ArrayList?

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[i]; 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
Oct 29 '06 #1
6 5517
Pavel Maly wrote:
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[i]; 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.
Oct 29 '06 #2
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<Tinstead of
ArrayList so that you won't need the cast:

List<intintList = outerList[1][2];

--
Dave Sexton

"Tom Spink" <ts****@gmail.comwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
Pavel Maly wrote:
>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[i]; 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.

Oct 29 '06 #3
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<Tinstead of
ArrayList so that you won't need the cast:

List<intintList = outerList[1][2];
Oct 29 '06 #4
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<ArrayListouter = new List<ArrayList>();

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

for (int j = 0; j < innerBound[i]; 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" <ac*****@domain.comwrote in message
news:uf****************@TK2MSFTNGP05.phx.gbl...
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<Tinstead of
ArrayList so that you won't need the cast:

List<intintList = outerList[1][2];

Oct 29 '06 #5
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<ArrayListouter = new List<ArrayList>();

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

for (int j = 0; j < innerBound[i]; 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];

Oct 29 '06 #6
Dave Sexton wrote:
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<Tinstead of
ArrayList so that you won't need the cast:

List<intintList = 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.

--
Hope this helps,
Tom Spink

Google first, ask later.
Oct 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tamir Khason | last post: by
6 posts views Thread by Michael C | last post: by
9 posts views Thread by Leon | last post: by
14 posts views Thread by Mike | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.