string[][] Tablero = new string[3][3];
I need to have a 3x3 array arrangement to save information to. How do I declare this in C#?
string[,] Tablero = new string[3,3];
You can also instantiate it in the same line with array initializer syntax as follows:
string[,] Tablero = new string[3, 3] {{"a","b","c"},
{"d","e","f"},
{"g","h","i"} };
You probably want this:
string[,] Tablero = new string[3,3];
This will create you a matrix-like array where all rows have the same length.
The array in your sample is a so-called jagged array, i.e. an array of arrays where the elements can be of different size. A jagged array would have to be created in a different way:
string[][] Tablero = new string[3][];
for (int i = 0; i < Tablero.GetLength(0); i++)
{
Tablero[i] = new string[3];
}
You can also use initializers to fill the array elements with data:
string[,] Tablero = new string[,]
{
{"1.1", "1.2", "1.3"},
{"2.1", "2.2", "2.3"},
{"3.1", "3.2", "3.3"}
};
And in case of a jagged array:
string[][] Tablero = new string[][]
{
new string[] {"1.1", "1.2"},
new string[] {"2.1", "2.2", "2.3", "2.4"},
new string[] {"3.1", "3.2", "3.3"}
};
You just declared a jagged array. Such kind of arrays can have different sizes for all dimensions. For example:
string[][] jaggedStrings = {
new string[] {"x","y","z"},
new string[] {"x","y"},
new string[] {"x"}
};
In your case you need regular array. See answers above.
More about jagged arrays
I assume you're looking for this:
string[,] Tablero = new string[3,3];
The syntax for a jagged array is:
string[][] Tablero = new string[3][];
for (int ix = 0; ix < 3; ++ix) {
Tablero[ix] = new string[3];
}
There are 2 types of multidimensional arrays in C#, called Multidimensional and Jagged.
For multidimensional you can by:
string[,] multi = new string[3, 3];
For jagged array you have to write a bit more code:
string[][] jagged = new string[3][];
for (int i = 0; i < jagged.Length; i++)
{
jagged[i] = new string[3];
}
In short jagged array is both faster and has intuitive syntax. For more information see: this Stackoverflow question
try this :
string[,] myArray = new string[3,3];
have a look on http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
string[,] Tablero = new string[3,3];
string[][] is not a two-dimensional array, it's an array of arrays (a jagged array). That's something different.
To declare a two-dimensional array, use this syntax:
string[,] tablero = new string[3, 3];
If you really want a jagged array, you need to initialize it like this:
string[][] tablero = new string[][] { new string[3],
new string[3],
new string[3] };
A 3x3 (multidimensional) array can also be initialized (you have already declared it) like this:
string[,] Tablero = {
{ "a", "b", "c" },
{ "d", "e", "f" },
{ "g", "h", "i"}
};
When you are trying to create a multi-dimensional array all you need to do is add a comma to the declaration like so:
string[,] tablero = new string[3,3].
you can also write the code below.
Array lbl_array = Array.CreateInstance(typeof(string), i, j);
where 'i' is the number of rows and 'j' is the number of columns.
using the 'typeof(..)' method you can choose the type of your array i.e. int, string, double
There are many examples on working with arrays in C# here.
I hope this helps.
Thanks,
Damian
Related
How can I add an array to another 2D array? For example
//change this
array2d = { {1,2,3}, {4,5,6} };
//to this
array2d = { {1,2,3}, {4,5,6}, {7,8,9} };
//by adding
array1d = {7,8,9};
Is there any better way other than create a new array, which is bigger than the old one, then copy the old array to the array?
I would recommend a List<List<int>> for something like this, unless you have a reason not to.
The code for it may look something like this:
List<List<int>> my2dList = new List<List<int>>()
{
new List<int>()
{
1,
2,
3
},
new List<int>()
{
4,
5,
6
},
};
my2dList.Add(new List<int>(){7,8,9});
Alternatively, if you really want to limit each column to a length of three, consider using an inner structure with an immutable size (array of size 3, etc)
It seems like you might want to use a different data structure, a list of arrays would make this much easier.
Extended version of the code snippet posted by #VP. includes the back-conversion from List(List> to Jagged Array using Linq:
// 2d array to List
List<List<int>> ar2list = new List<List<int>>()
{
new List<int>() { 1, 2, 3 },
new List<int>() { 4, 5, 6 },
};
// adding item to List
ar2list.Add(new List<int>() { 7, 8, 9 });
// List to Jagged array conversion using Linq
int[][] _arrConcat = ar2list.Select(Enumerable.ToArray).ToArray();
Finally, pertinent to this particular use-case as requested by OP, int[][] can be easily converted to int[,] with simple for loop:
array2d = new int[_arrConcat.Length, 3];
for (int i = 0; i<_arrConcat.Length; i++)
{
for (int j = 0; j < 3; j++)
{
array2d[i, j] = _arrConcat[i][j];
}
}
where array2d is the original array with added index/content.
Hope this will help. Best regards,
I want to initialize an array of arrays like in java:
int[][] arrPos=new int[16][48];
int[][] arrPosOther=new int[16][48];
and I can set a row array value like this:
arrPos[0]=arrPosOther[0];
and I can set a cell value like this:
arrPos[1][0]=125;
but in C#, I can declare only like this:
int[][] arrPos=new int[16][];
can not set the column value in the initialization.
It seems like you are trying to find a way to initialize Jagged array in c#: please refer to the following example:
int[][] jaggedArray2 = new int[][]
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
The short form for the same sample is shown below:
int[][] jaggedArray2 =
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
You can also perform the initialization in several steps:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
And, apparently, you can implement a sort of for or foreach loop in order to populate the array from some data structure. More reading available at: http://msdn.microsoft.com/en-us/library/2s05feca.aspx
Also, you should probably consider a use of multidimensional array, like int[,] (the C# syntax in this case is different from Java lang).
Hope this will help.
There is no syntax you are looking for.
One statement option could be
int[][] arrPos = Enumerable.Range(0, length).Select(_ => new int[length]).ToArray();
Lets say I have
int[] arraySegment1 = new int[10];
int[] arraySegment2 = new int[10];
Is there anyway to pass them into a one dimensional array by reference?
int[] array = new int[21];
//Could I now make the arraySegment1 be passed in array[0] - array[10] by reference?
//And arraySegment2 in array[11] - array[21] passed by reference?
//Then when executing:
array[0] = 10000000;
System.Console.WriteLine(arraySegment1[0]);
//It should display 10000000
//By putting the arraySegment1 as reference in the one dimensional array: array?
Checkout the Array.Copy method which allows you to copy segments of arrays to a destination array:
int[] arraySegment1 = new int[10];
int[] arraySegment2 = new int[10];
// TODO: populate the arraySegment1 and arraySegment2 with some values
int[] array = new int[20];
Array.Copy(arraySegment1, 0, array, 0, arraySegment1.Length);
Array.Copy(arraySegment2, 0, array, arraySegment1.Length, arraySegment2.Length);
Also 20 is enough of a Length for the resulting array, not 21 if the 2 source arrays are 10 each.
Try this one..
int[] arraySegment1 = new int[10];
int[] arraySegment2 = new int[10];
int[] array = new int[21];
arraySegment1.CopyTo(array,0);
arraySegment2.CopyTo(array,(arraySegment1.Length));
No. It's not possible with standart arrays. But with generics you can do something like this:
int[] arraySegment1 = new int[10];
int[] arraySegment2 = new int[10];
// populate arrays
List<int[]> lists = new List<int[]>();
lists.Add(arraySegment1);
lists.Add(arraySegment2);
lists[0][0] = 100000;
System.Console.WriteLine(arraySegment1[0]); // then it will display 100000
you typing wrong object
change the arraySegment1 in the write line to be array
System.Console.WriteLine(array[0]);
I have the following array:
int[] numbers;
Is there a good way to add a number to the array? I didn't find an extension method that concats 2 arrays, I wanted to do something like:
numbers = numbers.Concat(new[] { valueToAdd });
To concatenate 2 ararys look at: How do I concatenate two arrays in C#?
The best solution I can suggest is to simply use
List<int> numbers
And when needed call the ToArray() extension method (and not the opposite).
you can try this..
var z = new int[x.length + y.length];
x.CopyTo(z, 0);
y.CopyTo(z, x.length);
or
List<int> list = new List<int>();
list.AddRange(x);
list.AddRange(y);
int[] z = list.ToArray();
or
int[] array1 = { 1, 3, 5 };
int[] array2 = { 0, 2, 4 };
// Concat array1 and array2.
var result1 = array1.Concat(array2);
for (int i = 0; i < People.Length; i++) {
People[i] = new Person(first[i], last[i], birth[i]);
}
Now first and last contain 20 strings and birth is a DateTime object that again populates the array people with 20 birthdates. I just need to know how to correctly initialize my array.
You have to use Jagged Array
ex:
You can initialize jagged arrays like this example:
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
You can also omit the size of the first array, like this:
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
You initialize your array with the desired size:
Person[] people = new Person[20];
To automatically use the length of, for example, first:
Person[] people = new Person[first.Length];