Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

If I resize an array doing this (e.g.):
String names[] = new String [1];
int numberofnames = 8;

Array.Resize(ref names, numberofnames);


How can I do for an array with more than one dimension?:
//first column for names, second for places
String namesplaces[,] = new String[1,2]; 

I want to change the array to a new dimension such as [10,2] or even [20,5]; I mean, any number of rows and columns.

I ONLY want a .NET function just like the 'Array.Resize' given above... ¿Does it exist? Thanks
Posted

private void ResizeArray(ref string[,] original, int cols, int rows)
{
    //create a new 2 dimensional array with
    //the size we want
    string[,] newArray = new string[rows, cols];
    //copy the contents of the old array to the new one
    Array.Copy(original, newArray, original.Length);
    //set the original to the new array
    original = newArray;
}


// so this resizes the array to 10 columns and 2 rows..
ResizeArray(list,10,2);


hmm sorry :) I didnt read your last sentence so answer to it you may use;

System.Collections.ArrayList class, which is a one-dimensional array that resizes itself. If you have an ArrayList whose elements were all ArrayLists, that will work as a multidimensional, so then it will automatically resize the array.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 22-Feb-11 16:18pm    
I personally agree, but as OP asks _only_ about available analog of Array.Resize, this is not an answer. See my answer, for that matter.
Anyway, 5 from me.
--SA
Orcun Iyigun 22-Feb-11 16:23pm    
Yes you are right :) I have read it and updated it but still, it might not fully cover what OP wants.
pancho2413 22-Feb-11 16:43pm    
Hahaha easy guys. Thanks for helping me, I appreciate it a lot :-D. Hard to decide who won :-P
Orcun Iyigun 22-Feb-11 17:48pm    
it is not about winnig we are just trying to help :) SA always has good answers.
pancho2413 22-Feb-11 18:15pm    
All of you won from my point of view. You help people just because you feel good doing it, I offer the same too. I really, really thank you all.
It does not exist.

You can implement such function yourself for array of fixed dimension, but I'm not explaining how, because you probably know and ask only about what you asked. Also, such function would be slow.

You could use regular Array.Resize for jagged arrays (like string[][]), but I'm not explaining how, because you probably know and ask only about what you asked. By the same reason, I'm not explaining how to use collections like System.Collections.Generic.List.

—SA
 
Share this answer
 
Comments
pancho2413 22-Feb-11 16:54pm    
I think you wrote the correct answer. I know how to implement it as a function but I was wondering about the existence of a .net function I was missing. BUT you helped me with that System.Collections.Generic.List. Thanks
Sergey Alexandrovich Kryukov 22-Feb-11 17:48pm    
You're welcome. (Please see other containers from System.Collections.Generic, most of them are critically important.)
Thanks for accepting my Answer.
Good luck, call again.
--SA
jfriedman 22-Feb-11 18:03pm    
Very nice answer SA, beautiful response & beautiful analyzation of the question. You have forced me to rethink the way I answer questions in general.

Reguards,
Julius
Sergey Alexandrovich Kryukov 22-Feb-11 18:36pm    
Really? Thank you very much.
What's wrong with your Answers (may be I should rethink mine... :-).
--SA
pancho2413 22-Feb-11 18:17pm    
Sure, jfriedman is right. And I was looking for people who answer exactly what I ask :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900