473,387 Members | 1,516 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,387 software developers and data experts.

ArrayList of arrays

I have a function which returns array of structs. I need to create a
collection of those arrays and thought that an ArrayList would be a
good way to do this since the count is variable. The problem I am
having is that although the arrays are being properly stored in the
ArrayList, I cannot access the individual items in the arrays. For
instance I was hoping to do this:

public struct S
{
public int X;
public string Y;
}

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance[0]);
AL.add(SInstance[1]);

int xx = AL[0].SInstance[0].X;
string yy = AL[0].SInstance[0].Y;

As I step through the code, I can see all of the struct instances in
the array, but I don't know how to access them. Any ideas?

thanks,

Dec 14 '06 #1
3 9833
"Zenon" <ze****@comcast.netwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
>I have a function which returns array of structs. I need to create a
collection of those arrays and thought that an ArrayList would be a
good way to do this since the count is variable. The problem I am
having is that although the arrays are being properly stored in the
ArrayList, I cannot access the individual items in the arrays. For
instance I was hoping to do this:

public struct S
{
public int X;
public string Y;
}

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance[0]);
AL.add(SInstance[1]);

int xx = AL[0].SInstance[0].X;
int xx = (AL[0] as S).X;

.... etc.

Three problems. First problem is that the result of the ArrayList indexer
is of type object, not S, so you need the explicit cast to make it work.
Second is that you're not putting arrays into the array list, you're putting
inidividual structs into the ArrayList. If you really mean to put arrays
in, then you need something along the lines of the code below. Third is
that you're trying to use the identifier 'SInstance' in a way that doesn't
make sense. It's just the name of a local variable, not a type, or field or
property of some type.

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance]);

int xx = (AL[0] as S[])[0].X;
string yy = (AL[0] as S[])[1].Y;

.... etc.

-cd
Dec 14 '06 #2
Sorry I mistyped my example, I was putting arrays into the arraylist as

AL.add(SInstance);
AL.add(SInstance);

The explicit cast is what I needed, thanks.

Is it bad form to do what I am doing here? I was hoping to avoid a
rewrite, but at the same time, I don't want to write kludgy code.
thanks again

Carl Daniel [VC++ MVP] wrote:
"Zenon" <ze****@comcast.netwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
I have a function which returns array of structs. I need to create a
collection of those arrays and thought that an ArrayList would be a
good way to do this since the count is variable. The problem I am
having is that although the arrays are being properly stored in the
ArrayList, I cannot access the individual items in the arrays. For
instance I was hoping to do this:

public struct S
{
public int X;
public string Y;
}

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance[0]);
AL.add(SInstance[1]);

int xx = AL[0].SInstance[0].X;

int xx = (AL[0] as S).X;

... etc.

Three problems. First problem is that the result of the ArrayList indexer
is of type object, not S, so you need the explicit cast to make it work.
Second is that you're not putting arrays into the array list, you're putting
inidividual structs into the ArrayList. If you really mean to put arrays
in, then you need something along the lines of the code below. Third is
that you're trying to use the identifier 'SInstance' in a way that doesn't
make sense. It's just the name of a local variable, not a type, or field or
property of some type.

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance]);

int xx = (AL[0] as S[])[0].X;
string yy = (AL[0] as S[])[1].Y;

... etc.

-cd
Dec 14 '06 #3
Zenon wrote:
Sorry I mistyped my example, I was putting arrays into the arraylist
as

AL.add(SInstance);
AL.add(SInstance);

The explicit cast is what I needed, thanks.

Is it bad form to do what I am doing here? I was hoping to avoid a
rewrite, but at the same time, I don't want to write kludgy code.
There's nothing overtly wrong with doing this. It's not the greatest style,
as there are lots of ways to get it wrong (as you found). you might
consider wrapping this whole collection of arrays of structures into your
own custom collection class to reduce the likelihood of errors in correctly
accessing/updating the structure. If you can, move to .NET 2.0 (or are
you already using it?) which would allow you to use

List<S[] (or even List<List<S>if you prefer).

rather than the weakly typed ArrayList.

-cd
Dec 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vaden95 | last post by:
I am trying to uae ArrayList to store arrays of structures, but I can't get my data back after storing the arrays. My (highly shrunk and modified) code is below. The problem is at the line labled...
1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
3
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to...
5
by: Kevin | last post by:
Hi All Can someone please tell me the difference between an Array and an ArrayList, and where I would be likely to use one over the other Thanks alot for any help
4
by: Ricardo | last post by:
how can i acess the members of an array that i put into a arraylist, like this: int dez = new int; ArrayList arList = new ArrayList(); arList.add(dez); I want to get the dez value.
2
by: D. Shane Fowlkes | last post by:
I've been reading up on Arrays in ASP.NET. I'm going to create an two dimensional array of some type to contain 5 columns but a variable amount of rows. I read up on the ArrayList function and...
3
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it...
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
by: cowboyrocks2009 | last post by:
Hi. I want to return values from multiple Arraylist. How can I do that ? later I want to use these values in another class. Can somebody help class myClass{ public ArrayList<Rectangle>...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.