473,216 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

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 5769
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tamir Khason | last post by:
How can I set the type of the object added to ArrayList (type of Array List Members) Here is the code: protected ArrayList tabs = new ArrayList(); public ArrayList Tabs {
6
by: Michael C | last post by:
Is it possible to use an ArrayList inside a struct? I keep running into a null reference exception when I try to Add to the ArrayList in the struct, and it won't let me initialize the ArrayList in...
2
by: Ric | last post by:
Please forgive me if i dont explain this situation correctly. i'll try to do my best. because the arraylist.add method only accepts one argument, i created an object with several public...
9
by: Leon | last post by:
I have a webform in which when the user press generate button the form generate six unique ramdon numbers, the user can also type these six numbers in manually in any order. however, the user can...
14
by: Mike | last post by:
I had a question about threading and access to private class variables. I am developing a windows service which has a class inside of it which will receive various asynchronous calls to it via...
4
by: rjl | last post by:
Is there a way to assign an arraylist inside an arraylist as a column? i have arrayList list, which has 2 Strings and an arrayList with 3 values. I would like the following columns then in a...
1
by: dotnetnoob | last post by:
i have a arraylist that store String() array the string array hold value 1 2 4 i want to access the string array that is inside the arraylist one arraylist item at the time i search up and...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
20
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.