473,396 Members | 2,039 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,396 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 5781
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.