How do i initialize int[][]? - c#

In C# how do i initialize
int[][] test;
I don't want to use int[,] instead. I am asking specifically how to initialize the above. The obvious = int[1][1] does not work. I tried a few different ways with no luck and [][] is not really google-able unfortunately (unless its possible!?)

It's called jagged arrays if you want to Google it.
Basically you can initialize the first dimension in the traditional way :
int[][] test = new int[23][];
And you manually initialize the others :
for (int i = 0; i < test.Length; ++i)
test[i] = new int[42];

You need a for-loop to initialize an array of array.
int[][] test = new int[N][];
for (int i = 0; i < test.Length; i ++)
test[i] = new int [M];

int[][] scores = new int[5][];

This is called a Jagged Array. So basically you have initialize the "first dimension" first and then each element:
int[][] foo = new int[3][];
foo[0] = new int[2];
...

Related

How to append arrays in c#

I would like to get the output of this to be [1,2,3,4,...,200]. Any suggestions for how to go about this?
var Laser_data = 0;
var i = 0;
var j = 1;
int[] LaserData_200 = new int[200];
for (i = 0; i < LaserData_200.Length; i++)
{
Laser_data += j;
LaserData_200[i] = Laser_data;
Console.WriteLine(" " + LaserData_200[i]);
}
Current output:
1
2
3
4
ect.
Your array initialization and element assignment can be simplified massively. Your array is just the numbers 1 through 200 (inclusive). Enumerable.Range can generate that for you, then save it as an array.
int[] myArray = Enumerable.Range(1, 200).ToArray();
To print it all, string.Join it using a comma as a seperator.
Console.WriteLine($"[{string.Join(',', myArray)}]");
// [1,2,3,4,5,6,7,8,9,10,11,12,13, .... 200]
I see the title has nothing to do with the posted code.
So I am answering the question in the title.
Say you have two arrays a and b and you want to create a third array that combines the two arrays, then you write code like
int[] c = Enumerable.Concat(a, b).ToArray();
or you have and array a and you want to keep adding values to it in loop. When arrays are fixed size (a.IsFixedSize = true always) so you can do this efficiently.
The best solution is to use List<T> instead of an array
List<int> a = new List<int>()
for(int i=0; i<200; i++)
{
a.Add( i+1 );
}
and if you want an array in the end, you just do
int[] c= a.ToArray();

How to declare a multi-multi- dimensional array

Sounds simple and it probably is.
I have this variable:
byte[,,] data = new byte[360,288]
And I want 4 for them.
I do not want this though:
byte[,,,] data = new byte[360,288,4]
I prefer this:
byte[,,][] data = new byte[360,288][4]
Is it possible?
Yes this is a special case of jagged arrays, one where one of the jagged dimension is multidimensional.
You should write something like this:
// Initialise 4 arrays of two dimensional arrays
byte[][,] data = new byte[4][,];
// Initialise the arrays
for (var i = data.GetLowerBound(0); i <= data.GetLowerBound(0); ++i)
data[i] = new byte[360, 258];
Of course you can invert the dimensions if you need it.
// Initialise 4 arrays of two dimensional arrays
byte[,][] data2 = new byte[360,258][];
// Initialise the arrays
for (var i = data2.GetLowerBound(0); i <= data2.GetLowerBound(0); ++i)
for (var j = data2.GetLowerBound(1); j <= data2.GetLowerBound(1); ++j)
data2[i,j] = new byte[4];

Save and load initial state of an array (int[,])

I'm trying to save the state of an array so I can load it later in it's initial state. But I don't know how to make them separate instances, and not to reference each other. Here's some sample code:
static void Main(string[] args)
{
int[,] first = new int[5, 5];
int[,] second = first;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
first[i, j] = i * j;
}
}
first[0, 0] = 10000;
first = second;
Console.WriteLine(first[0, 0]); //10000
}
int[,] second = first;
Means that the second array is a reference to first, they are the same object. You need to create a new array instance. You mention that you want to save the state of the array for later use and for this, you have to copy your original array like so:
int[,] first = new int[5, 5];
int[,] second = new int[5, 5];
Array.Copy(first, second, first.Length);
If you want a separate instance, you need to instantiate it:
int[,] second = new int[5, 5];
Many ways of copying an array can be found here: Any faster way of copying arrays in C#?
first = second
Only copies the reference. You need to copy the elements one by one, just like the way you populate the first array.
Create a shallow copy with Array.Clone()

Create specific multidimensional array

I have an somewhat basic question regarding multidimensional arrays in C#.
Currently I have the following array:
float[] matrix = new float[16];
I need to create instead a 2D array with each line containing the previously mentioned 16 float numbers. Also, the number of lines in the 2D array is not known at the start of the program (i.e. it will be based on a variable).
How can I create such an array using an efficient data structure?
You could do something like this:
const Int32 arraySize = 16;
var list = new List<float[]>();
Which gives you an empty list containing zero elements (arrays) to start. As you need to add new arrays you would write this:
var array = new float[arraySize];
// do stuff to the array
And then add it to the list:
list.Add(array);
To store 16 float numbers, you could use a 4x4 matrix (which is a 4x4 2-dimensional array). For more details, check out this documentation.
// init the array
float[,] matrix = new float[4,4];
// loop through the array
for(int col = 0; col < matrix.GetLength(0); col++)
for(int row = 0; row < matrix.GetLength(1); row++)
Console.WriteLine(matrix[col, row]);
You can use multidimentional array syntax
float[,] arr2D = new float[12,12];
Alternatively, you could use a loop
float[][] floats = new float[12][];
for(int i=0; i< 12; i++)
{
floats[i] = new float[12];
}

Arrays Error Error "Invalid rank specifier: expected ',' or ']"

I'm doing my homework. I am assigned to create a Tic Tac Toe application, I've finished it but there's a small error, and I think I'm overlooking something, but I can't figure it out and need some new eyes to check it out because I think I'm looking at it for too long and missing a big concept.
Random r = new Random();//Creates the number generator.
int [][] numbers = new int [3][3]; //creates a 3x3 integer grid.
for (int i = 0; i< numbers.Length; i++){
for (int j=0;j<numbers[i].Length;j++){
numbers[i][j] = r.Next(2);//Traverses the array and inputs a number between 0 and 1 here.
}
}
Console.WriteLine("%d|%d|%d\n",numbers[0][0],numbers[0][1],numbers[0][2]);
Console.WriteLine("------");
Console.WriteLine("%d|%d|%d\n",numbers[1][0],numbers[1][1],numbers[1][2]);
Console.WriteLine("------");
Console.WriteLine("%d|%d|%d\n",numbers[2][0],numbers[2][1],numbers[2][2]);
The error is in the second line. Maybe I did the int [][] numbers = new int [][]; format wrong? Please help me identify what's wrong it. Thank you.
Try this when declaring your array:
int [,] numbers = new int[3,3];
You need to change your accesses to the array also fx:
numbers[i,j] = r.Next(2);
To iterate the array you can use numbers.GetLength(index) method instead of numbers.Length and numbers[i].Length.
In your case it would be something like this:
for (int i = 0; i < numbers.GetLength(0); i++)
{
for (int j = 0; j < numbers.GetLength(1); j++)
{
...
EDIT: If you really want to stay with your array of arrays (also called jagged arrays) you can initialize it like this:
int[][] numbers = new int[3][]
{
new int[3],
new int[3],
new int[3]
};

Categories

Resources