Array Size

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

Do I have to know the exact number of occurences of an array object when I
declared it, or can I add just declare the object and do not have to
declare the length?

For instance...

TableRow[] myTableRows;

Table row1 = new TableRow();
myTableRows[0] = row1;
Table row2 = new TableRow();
myTableRows[1] = row2;

I think I had to know the size of my array - at least in Java I did, so I
guess it is the same with C#?

If I do not know the size exactly, is it best for me to choose a number
that I know it will not exceed (small number like 10) or should I just use
a hashtable to store all of my row in? Or is there a better object ot use?
 
To resize an array you essentially need to create a new array of the
proper size and copy over the elements of the old array.

If you need dynamic add/remove capabilities, you might want to look into
the ArrayList. It does appropriate resizing for you under the covers.

For more info on arrays and ArrayLists, check out:

An Extensive Examination of Data Structures: Part 1
http://msdn.microsoft.com/vcsharp/d...us/dv_vstechart/html/datastructures_guide.asp

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
Here's another option. If you don't know exaclt how many items you're going
to end up with, you can start with an arrayList collection and then, when
you're done, turn it into as array if neded.

ArrayList myTableRows=new ArrayList();
Table row1 = new TableRow();
myTableRows.Add(row1);
Table row2 = new TableRow();
myTableRows.Add(row2);
TableRow[] theTableRowArray;
theTableRowArray=myTableRows.ToArray(TableRow);
 
Back
Top