I have a 2d array a[3,3]
. How can I express one dimension as a new array and pass it to some function?
int[,] a = new int[3,3];
a[0,0] = 1;
...
string b = concatenate(a[0]); // where concatenate is a function
// take a one dimension array as param
Also, can I create a 65000x65000 array with C#? I got some "out of memory" error.
The easiest way to handle this is to create a jagged array
int[][] i = new int[3][];
that way:
string b = concatenate(i[0]);
will work.
To your second question you will run into issues with the LOH with objects approaching that size. This is probably not your problem though. I would look here as to a possible explanation as to why.
You will need to use jagged arrays .
int[][] a = new int[3][]
a[0] = new int[3];
a[1] = new int[3];
a[2] = new int[3];
a[0][0] = 1;
string b = concatentate(a[0]);
Also, creating a 65000x65000 array would result in 65000^2 = 4225000000 slots (or about 16GB or data) so it is no wonder that you are getting an OutOfMemoryException
.
To answer your second question, a 65k x 65k two-dimensional array has more than 4 billion items; a 4-byte integer type would clock in at about 16GB in size. If you don't have enough memory on your box to initialize a 16GB block of memory, then, no, you cannot create a 65k x 65k array. And that's not even bothering with the addressing issues you'll run into trying to do a lookup on that array. Too big, man. Too big.
To answer your first question, a two-dimensional array is a unique construct that is incompatible with one-dimensional arrays. If you create a jagged array instead (which is an array of arrays), you'll be able to do what you want.
A 65000 by 65000 array of int
s would take about 16 gigabytes of memory, so it's not surprising you got an out of memory error. I couldn't get a single .NET 3.5 application to allocate more than about 1.7GB of memory when I last tried, so no luck there.
As for passing one dimension to a function, it depends. You may be able to do what you want with a jagged array :
int[][] array = new int[3][];
for (int i = 0; i < array.Length; i++)
array[i] = new int[3];
string b = concatenate(array[0]);
That way each position in the first dimension is its own array, which you can pass around (as in the last line). If you wanted to pass a row of items in the other 'direction', you'd have to copy items around first.
PS: You might want to look at String.Join
and String.Concat
for your concatenating needs. The second has a lot of overloads, but you probably need the first for displaying integers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.