Dynamic Array
-
Hi, How to I allocate a dynamic array in run time. I want to allocate an array, but length of array unfixed. Please help me. Thanks. H.Dung
-
Hi, How to I allocate a dynamic array in run time. I want to allocate an array, but length of array unfixed. Please help me. Thanks. H.Dung
Dung, what do you mean by "length of array unfixed" ? If you mean that you will not know the length until run-time, consider the following snipplet: int x = 3; char[] myArray = new char[x]; This compiles, and thus, even if x was determine by picking user input (or cfg file, or what have you), you should be able to use it. If, on the other hand, you need an array whose size will change in time (i.e. starts with 10 elements in it, but later on you need to add 5 more, and then shrink it to 5, etc etc) then arrays are not the correct data structure for you. Arrays have "fixed size" (they do not grow in time), but if you look at the classes in System.Collections you should find something for you. I often use the Hashtable class (but it's often an overkill!). HTH, Frank
-
Hi, How to I allocate a dynamic array in run time. I want to allocate an array, but length of array unfixed. Please help me. Thanks. H.Dung
You can simply use ArrayList class. Use its
Add
andRemove
methods. this way, you don't need to care about array size, it's completely dynamic I hope this helps :rose:
Don't forget, that's
Persian Gulf
not Arabian gulf!
-
You can simply use ArrayList class. Use its
Add
andRemove
methods. this way, you don't need to care about array size, it's completely dynamic I hope this helps :rose:
Don't forget, that's
Persian Gulf
not Arabian gulf!
Hi, Thank you very much. I have been used the ArrayList class in System.Collections namespace, and I have everything which I need. H.Dung.