.NET Forum / Languages / C# / June 2007
Use of ArrayList
|
|
Thread rating:  |
stainless - 21 Jun 2007 09:21 GMT I am not very experienced with C# and have a question regarding storing and accessing data in an array list.
Whilst reading a datarow sorted on 3 fields (thID, dthID and locID - all 3 are type int), I need to store unique combinations of the 3 values in an arraylist, and then be able to access the combinations later. I am fine with the reading of the datarow and identifying the unique combinations, but am struggling with how I would then store these in the array list.
For example:
thID dthID locID ----- ------ -------
1 1 1 2 1 1 2 1 2 2 2 2 2 4 2 3 4 3
How would I define the arraylist, add the data each time and then access these values later?
Cheers
Mark
Farrukh - 21 Jun 2007 10:28 GMT As far I understand from your question, you may need to use Two Dimensional Array or may be Multidimensional array according to the requirements: Here is a Raw example which might be helpful to you:
using System;
class TwoD { public static void Main() { int t, i; int[,] table = new int[6, 3]; //Declaring the array /* This is the array named "table" of 6 Rows and 3 Columns/Fields i.e. for each row there are 3 columns/fields. You can increase the number of rows according to your data size.
for each row there would be a combination of 3 columns. */
// Data for Row 1 table[0,0]=1; table[0,1]=1; table[0,2]=1
// Data for Row 2 table[1,0]=2; table[1,1]=1; table[1,2]=1
// Data for Row 3 table[2,0]=2; table[2,1]=1; table[2,2]=2
// go on up to 5 table[5,0]=5; table[5,1]=1; table[5,2]=3
} } }
Here is an illustration for this:
Columns thID | dthID | locID Rows------------------------------------------- R1 1 | 1 | 1 R2 2 | 1 | 1 R3 2 | 1 | 2 R4 2 | 2 | 2 R5 2 | 4 | 2 R6 3 | 4 | 3
You can use some Loop statement like "for" to access the combinations Hope this helps.
> I am not very experienced with C# and have a question regarding > storing and accessing data in an array list. [quoted text clipped - 24 lines] > > Mark stainless - 21 Jun 2007 10:36 GMT Thanks
This may work but I understood that an array has to be defined as a fixed size up front (in your answer, you define the array at start of processing as int[,] table = new int[6, 3]). Thus, if I knew there would be a maximum of 6 occurrences, this would work.
However, I do not know how many occurrences there are of the 3 values (my example was 6 but until I have built the list, I will not know what the size needs to be), thus I need to be able to add to the end of the list. This is why I thought an arraylist would be the better option as, if I understand correctly, this does not have to be defined as a fixed size up front.
Cheers
Mark
> As far I understand from your question, you may need to use Two > Dimensional Array or may be Multidimensional array according to the > requirements: Farrukh - 21 Jun 2007 10:56 GMT Mark,
I'm sorry, that example was just to give an idea, even to myself :)
I suggest, you should use structure and define your ArrayList in that structure..
> Thanks > [quoted text clipped - 17 lines] > > Dimensional Array or may be Multidimensional array according to the > > requirements: Peter Bromberg [C# MVP] - 21 Jun 2007 11:49 GMT Stainless, An ArrayList holds type Object, there is nothing to define. If you don't know how many elements each of your "objects" will have, you can even store individual ArrayList instances in each main ArrayList element.
Peter
 Signature Site: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com Short urls & more: http://ittyurl.net
> I am not very experienced with C# and have a question regarding > storing and accessing data in an array list. [quoted text clipped - 24 lines] > > Mark Rick Lones - 21 Jun 2007 12:40 GMT > I am not very experienced with C# and have a question regarding > storing and accessing data in an array list. [quoted text clipped - 20 lines] > How would I define the arraylist, add the data each time and then > access these values later? Not entirely clear what you are trying to do - is it something like:
class RowKey { int thID; int dthID; int locID;
void RowKey(th, dth, loc) { thID = th; dthID = dth; locID = loc; } }
ArrayList list = new ArrayList(); list.Add(new RowKey(1,1,1)); list.Add(new RowKey(2,1,1));, etc. ??
You could then use constructs like: foreach (RowKey rk in list) { . . . } or RowKey rk = (RowKey)list[i], etc.
If this is what you are after (more or less), then you could look into List<RowKey> (the generic). Also, if you want to preserve the order by sorting your list you may want to implement IComparable on the RowKey class . . .
HTH, -rick-
stainless - 22 Jun 2007 08:05 GMT Thanks everyone
I think I have the solution now based on Farrukh's suggestion of using structs. I have defined a struct as below:
public struct UniqueLocation { public int alocID; public int dthID; public int thID;
}
Then, I can add populated instances of this struct to my arraylist e.g.
arrayListname.Add(currentLocation);
I have to admit I haven't fully tested this yet (due to other C# issues I am working through) but the concept looks fine.
Cheers
Mark
Peter Duniho - 22 Jun 2007 08:16 GMT > [...] > Then, I can add populated instances of this struct to my arraylist [quoted text clipped - 4 lines] > I have to admit I haven't fully tested this yet (due to other C# > issues I am working through) but the concept looks fine. It probably is fine. However, you should definitely consider Rick's suggestion to use the List<> generic class, especially since you are storing the data in structs instead of classes. You can declare an appropriate instance like this:
List<UniqueLocation> arrayListname = new List<UniqueLocation>();
IMHO, the primary advantage is that, if I recall correctly, using the List<> generic class there is no boxing/unboxing overhead when using a struct type (this wouldn't be an issue if you were using a class instead of a struct), as there would be when using the ArrayList class. Another advantage is the compile-type type-checking you get from using the generic class.
Pete
Jon Skeet [C# MVP] - 22 Jun 2007 18:41 GMT > Thanks everyone > [quoted text clipped - 8 lines] > > } Not good on three fronts:
1) Do you really want the locations being boxed and unboxed all over the place? Why did you decide to use a struct instead of a class?
2) Using public fields is a recipe for disaster
3) Mutable value types are a bad idea - they're likely to cause confusion, particularly when you combine them with boxing.
I'd echo the other posters: use a List<T>, and define a class with appropriate members, properly encapsulated in properties.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|