Related
My problem is I want to initialize a multidimensional array declared like this:
int[][][] my3DArray;
However, the following code gives me an error on [sizeY][sizeZ] saying it expected ',' or ']'.
void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
my3DArray = new int[sizeX][sizeY][sizeZ];
}
When i declare the array like this though:
int[,,] my3DArray;
I am able to initialize it without any problems by doing it like this:
my3DArray = new int[sizeX,sizeY,sizeZ];
But then the problem becomes that if I try to get the length of one of the arrays I can't do for example my3DArray[x,y].Length and am instead forced to pass all of the indexes together my3DArray[x,y,z].
So, is there any way I can initialize the array with it being declared as int[][][] my3DArray;? Or will I have to store the sizes of the arrays elsewhere and use it like this int[,,] my3DArray;?
You can do:
new int[][][] myArray = new int[][][]
{
new int[][]
{
new int[]
{
// Your numbers
}
}
}
myArray[0].Length
myArray[0][0].Length
myArray[0][0][0].Length
Works fine
int[][] is a jaggad array, which means that every array can be in a different size.
This is why you can initialize it that way:
int[][] arr2D= new int[3][];
arr2D[0] = new int[0];
arr2D[1] = new int[1];
arr2D[2] = new int[2];
which will create a 2d array that looks like this:
_
_ _
_ _ _
The following is an example to create 3d jaggad array (with different sizes for each dimension- 5 rows, 3 columns and 6 depth):
int[][][] arr3D = new int[5][][];
for (int col = 0; col < arr3D.Length; col++)
{
arr3D[col] = new int[3][];
for (int depth = 0; depth < arr3D[col].Length; depth++)
{
arr3D[col][depth] = new int[6];
}
}
In order to get the dimension size in jaggad array, you can simply get the lenth of the array, but keep in mind that if the array is actually jagged, you will have different sizes for different arrays. if you initialize all of the arrays in a specific dimension with the same value, than you can check either of them, it will be the same.
int[,] is a multi dimensional array that every dimension have a fixed size.
To create a multi dimensional:
int[,] arr2D = new int[3,4];
which will create a 2d array looking like this:
_ _ _ _
_ _ _ _
_ _ _ _
In multi dimensional array you can't change the length of a specific row or column.
In most cases you will probably prefer a multi dimensional array.
In order to get the size of a specific dimension in a multi dimensional array, you can use the following method:
int rows = arr22.GetLength(0); int cols = arr22.GetLength(1); int depth = arr22.GetLength(2);
The input is the dimension you want the size of.
You can't declare a jagged array in one line like this: new int[sizeX][sizeY][sizeZ]; as you really have three levels of nested arrays. You need to initialize them at each level.
This is what that would look like:
void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
my3DArray = new int[sizeX][][];
for (var x = 0; x < sizeX; x++)
{
my3DArray[x] = new int[sizeY][];
for (var y = 0; y < sizeY; y++)
{
my3DArray[x][y] = new int[sizeZ];
}
}
}
Now, if you do want to initialize a multidimensional array, you can do this:
int[,,] z =
{
{ { 1, 2, }, { 1, 2, }, { 1, 2, }, },
{ { 1, 5, }, { 1, 5, }, { 2, 8, }, },
{ { 1, 5, }, { 1, 5, }, { 2, 8, }, },
{ { 1, 5, }, { 1, 5, }, { 2, 8, }, },
};
And then, to get the dimensions, you can do this:
Console.WriteLine(z.GetLength(0));
Console.WriteLine(z.GetLength(1));
Console.WriteLine(z.GetLength(2));
That gives me:
4
3
2
It's important to note that with a jagged array each nested array can be of different length, but with a multidimensional array then each dimension must be uniform.
For example, a jagged array could look like this:
int[][] y = new int[][]
{
new [] { 1, 2, },
new [] { 1, 2, 3, },
new [] { 1, },
};
And a multidimensional array like this:
int[,] y =
{
{ 1, 2, 3, },
{ 4, 5, 3, },
{ 1, 6, 3, },
{ 1, 7, 8, },
};
I could not do this:
//illegal!!!
int[,] y =
{
{ 1, 2, 3, },
{ 4, 5, 3, 7, },
{ 1, 6, },
{ 1, 7, 8, },
};
First,
int[][][]
is a jagged array, or an array of arrays of arrays. Where as,
int [,,]
is a multi-dimensioinal array. A single array with many dimensions.
While multi-dimensional arrays are easier to instantiate and will have fixed dimensions on every axis, historically, the CLR has been optimized for the vastly more common case of single dimensional arrays, as used in jagged arrays. For large sizes, you may experience unexpected performance issues.
You can still get the dimensions of a multi-dimensional array by using the GetLength method, and specifying the dimension you are interested in but, its a little clunky.
The number of dimensions can be retrieved from the Rank property.
You may prefer a jagged array and instantiating one axis at a time.
To instantiate jagged arrays, see this Initializing jagged arrays.
void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
my3DArray = Enumerable.Repeat(Enumerable.Repeat(new int[sizeZ], sizeY).ToArray(),sizeX).ToArray();
}
I want to make an array which one dimension's length is defined but the other one is undefined
I try to do it with "LIST" and I dont know how to make a two dimension array which I already know how much is the length of one dimension
Make an array of lists. But make sure you include System.Collections.Generic in your class:
using System;
using System.Collections.Generic;
in the body of the method or class where you need this:
var arrayOfList = new List<int>[10];
for (var i = 0; i < 10; i++)
{
// initialize each entry of the array
arrayOfList[i] = new List<int>();
}
// add some stuff to entry one at a time
arrayOfList[0].Add(1);
// add some integers as a range
arrayOfList[0].AddRange(new [] {2, 3, 4});
// add some stuff to entry 1
arrayOfList[1].AddRange(new [] {5, 6});
If you don't want to use the generic list class, you can do a jagged array but it isn't as nice to deal with.
int[][] jaggedArray = new int[3][];
// initialize before using or else you'll get an error
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
// populate them like this:
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
See more here: http://msdn.microsoft.com/en-us/library/2s05feca.aspx
Is there any way to do the following?
int[,] multiArray = new int[5,5];
multiArray[0] = {0, 1, 3, 4, 5};
No, with multidimensional arrays, this is not possible. The array has a fixed size, and the compiler does not now what size you are allowed to assign to the array.
Also, how would the compiler know if you meant to do this:
multiArray[0, 0..4] = { 1, 2, 3, 4, 5 };
or this:
multiArray[0..4, 0] = { 1, 2, 3, 4, 5 };
However, you can use jagged arrays:
int[][] multiArray = new int[5][];
multiArray[0] = new[] { 1, 2, 3, 4, 5 };
I think what you want to do is save each element of the array into the same row but DIFFERENT columns of the multiArray.
multiArray[0][0] = 0;
multiArray[0][1] = 1;
multiArray[0][2] = 2;
multiArray[0][3] = 3;
multiArray[0][4] = 4;
multiArray[0][5] = 5;
I believe that is what you're trying to do. You can simplify this with a for loop as well.
Since .NET multidimensional arrays are stored in row-major order, you can use Buffer.BlockCopy for this operation (filling a full row at a time). Note the use of sizeof(int) since this method operates on bytes, not elements (unlike methods such as Array.Copy).
int[,] multiArray = new int[5, 5];
int rowIndex = 0;
int[] rowData = { 0, 1, 3, 4, 5 };
int destOffset = rowIndex * sizeof(int) * multiArray.GetLength(0);
Buffer.BlockCopy(rowData, 0, multiArray, destOffset, rowData.Length * sizeof(int));
I have an array with elemnents in order 1,2,3,4,5 and I would need to reverse it so it will be 5,4,3,2,1.
What about the following pseudo code? Is here not an easier way
EDIT: I Am sorry I thought multidimensional array
someclass [,] temporaryArray=new someclass [ArrayLenght,ArrayLenght];
//for each dimension then
for(int I=0;I<ArrayLenghtOfDimension;I++)
{
temporaryArray[ArrayLenghtOfDimension-I]=Array[I];
}
Array=temporaryArray;
The array base class has a Reverse() extension method built in
int[] originalArray = new int[] { 1, 2, 3, 4, 5 };
int[] reversedArray = originalArray.Reverse().ToArray();
Note that the Reverse method returns IEnumerable, so you need to call ToArray() on the result.
And if you need to just iterate over the elements in the array, then all you need is
foreach (int element in originalArray.Reverse())
Console.WriteLine(element);
Oops - Reverse is on IEnumerable, not Array, so you can use that with any collection.
IEnumerable<int> IEnumerableInt = new List<int>() { 1, 2, 3 };
int[] reversedArray2 = IEnumerableInt.Reverse().ToArray();
Yes there is fast solution exists in .net
int[] values = new int[] { 1, 2, 3, 4, 5 };
Array.Reverse(values);
Your array is reversed. so you can iterate through it
foreach (int i in values)
{
Response.Write(i.ToString());
}
the above code will display
54321
It will also work for string[], char[] or other type of arrays
Event though the Array class has Reverse methods defined:
Array.Reverse(originalArray); // original array is now reversed
If all you need to do is iterate backwards over it do the following:
for(int I= ArrayLength - 1; I >= 0; I--)
{
}
This avoid re-allocating memory for the reversed array.
Array.Reverse is the best way to do this. Do you care about order of the elements at all? If so,then you can do the following.
int[] originalArray = new int[] { 10, 2, 13, 4, 5 };
int[] descOrderedArray = originalArray.OrderByDescending(i => i).ToArray();
int[] ascOrderedArray = originalArray.OrderBy(i => i).ToArray();
For a multi-dimensional array it's the same idea
int[][] multiDimArray = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
int[][] reversedMultiArray = multiDimArray.Reverse().ToArray();
produces an array of two arrays that is: {4, 5, 6}, {1, 2, 3}
How can I copy an array of values to a destination array starting from a specific index without looping?
For example,if I have an array with 2 values, I have to copy those two elements to another array which has a capacity of 5 starting from index 3?
double[] source = new double[] {1, 2};
double[] destination = new double[5]{0,0,0,0,0};
//How to perform this copy?
double[] result = new double[5] {0, 0, 0, 1, 2};
Is this what you're looking for?
Array.Copy(source, 0 /*start loc*/, destination, 3 /*start loc*/, 2 /*count*/);
Use Array.CopyTo or the static method Array.Copy.
source.CopyTo(destination, 3);
double[] source = new double[] {1, 2};
double[] destination = new double[5]{0,0,0,0,0};
//How to perform this copy?
ArrayList result = new ArrayList();
result.AddRange(source);
result.AddRange(destination);
destination = result.ToArray();