473,626 Members | 3,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(SInstanc e[0]);
AL.add(SInstanc e[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 9851
"Zenon" <ze****@comcast .netwrote in message
news:11******** *************@j 72g2000cwa.goog legroups.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(SInstanc e[0]);
AL.add(SInstanc e[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(SInstanc e]);

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(SInstanc e);
AL.add(SInstanc e);

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******** *************@j 72g2000cwa.goog legroups.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(SInstanc e[0]);
AL.add(SInstanc e[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(SInstanc e]);

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(SInstanc e);
AL.add(SInstanc e);

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
3307
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 Line 1. I get an exception thrown saying: "An unhandled exception of type 'System.InvalidCastException' occurred in SLAP.exe Additional information: Specified cast is not valid." I have a watch at line 1 for al, and before the line is run, al...
1
4614
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 project I work on uses Xml serialization to store game objects for loading. One of the techniques we use is to make an array property and use the set block to perform actions on the values after they're loaded from the xml, as in: public string Ids
3
1889
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 pItem. As I understand it (and I coded it to test) if I try to use pAttribute again by clearing with pAttribute.Clear because it is a shallow copy the pItem is also cleared. I guess I need a "deep copy" but can't find any documentation on it. ...
5
7854
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
1764
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
2571
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 the HashTable as well. In the two books I have, there's no mention of the limitations of an ArrayList and it can only contain a single column array. But from the examples, I've seen, this seems to be true. The HashTable, as far as I can tell, can...
3
2969
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 would be fairly easy to add these different double arrays to different ArrayLists and then use ToArray to get all the numbers back in one array. Sort of a cheap concatenate so I don't need to loop through all the points or keep newing larger arrays
20
3735
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 do this. My tab delimited Input File looks like this:- 21 p 13e 0 62 1 580001 andrew -14.53 -13.95 0 0 21 p 13d 63 124 580002 1160001 andrew -13.95 -13.37 0 0 21 p 12g 311 364 2900000 3385714 john -11.63 -11.14 0 0 21 q 11.1a 1274 1321...
1
3005
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> readFile()throws Exception { String n = null; // List draw; try{ BufferedReader fh = new BufferedReader(new FileReader("myInputFile.txt")); while(true){
0
8641
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.