I'm trying to declare this kind of variable:
float[][]
Things that didn't work for me (wouldn't compile) -
float[][] inputs = new float[10][5];
float[][] inputs = new float[10, 5];
When trying to declare the array like this -
int a = 3;
int b = 2;
float[][] inputs = new float[][]
{
new float[a],
new float[b]
};
I get a multidimensional array with two float arrays instead of an array that has 3 arrays and every array size is 2.
Well, there are two different types:
Array of array (jagged array):
float[][] sample = new float[][] {
new float[] {1, 2, 3},
new float[] {4, 5}, // notice that lines are not necessary of the same length
};
2d array:
float[,] sample2 = new float[,] {
{1, 2, 3},
{4, 5, 6},
};
Edit: your code amended:
// jagged array (10 arrays each of which has 5 items)
float[][] inputs = new float[10][] {
new float[5],
new float[5],
new float[5],
new float[5],
new float[5],
new float[5],
new float[5],
new float[5],
new float[5],
new float[5],
};
You can shorten the declaration with a help of Linq:
float[][] inputs = Enumerable
.Range(0, 10) // 10 items
.Select(i => new float[5]) // each of which is 5 items array of float
.ToArray(); // materialized as array
Or in case of 2d array
// 2d array 10x5
float[,] inputs = new float[10,5];
Related
I am new to C# programming, I migrated from python. I want to append two or more array (exact number is not known , depends on db entry) into a single array
like the list.append method in python does. Here the code example of what I want to do
int[] a = {1,2,3};
int[] b = {4,5,6};
int[] c = {7,8,9};
int[] d;
I don't want to add all the arrays at a time. I need somewhat like this
// I know this not correct
d += a;
d += b;
d += c;
And this is the final result I want
d = {{1,2,3},{4,5,6},{7,8,9}};
it would be too easy for you guys but then I am just starting with c#.
Well, if you want a simple 1D array, try SelectMany:
int[] a = { 1, 2, 3 };
int[] b = { 4, 5, 6 };
int[] c = { 7, 8, 9 };
// d == {1, 2, 3, 4, 5, 6, 7, 8, 9}
int[] d = new[] { a, b, c } // initial jagged array
.SelectMany(item => item) // flattened
.ToArray(); // materialized as a array
if you want a jagged array (array of arrays)
// d == {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
// notice the declaration - int[][] - array of arrays of int
int[][] d = new[] { a, b, c };
In case you want to append arrays conditionally, not in one go, array is not a collection type to choose for d; List<int> or List<int[]> will serve better:
// 1d array emulation
List<int> d = new List<int>();
...
d.AddRange(a);
d.AddRange(b);
d.AddRange(c);
Or
// jagged array emulation
List<int[]> d = new List<int[]>();
...
d.Add(a); //d.Add(a.ToArray()); if you want a copy of a
d.Add(b);
d.Add(c);
It seems from your code that d is not a single-dimensional array, but it seems to be a jagged array (and array of arrays). If so, you can write this:
int[][] d = new int[][] { a, b, c };
If you instead want to concatenate all arrays to a new d, you can use:
int[] d = a.Concat(b).Concat(c).ToArray();
var z = new int[x.Length + y.Length];
x.CopyTo(z, 0);
y.CopyTo(z, x.Length);
You can use Array.Copy. It copies a range of elements in one Array to another array. Reference
int[] a = {1,2,3};
int[] b = {4,5,6};
int[] c = {7,8,9};
int[] combined = new int[a.Length + b.Length + c.Length];
Array.Copy(a, combined, a.Length);
Array.Copy(b, 0, combined, a.Length, b.Length);
Array.Copy(c, 0, combined, a.Length, b.Length, c.Length);
I have several arrays in C# in this form:
int[ ,] A1 = new int[3, 3]
{ { 0, 0, 0},
{ 1, 1, 1},
{ 0, 0, 0}
};
and am interested in finding out how I can store them in another array so that I can access these 2 dimensional arrays.
Thanks
You can declare an array of multi dimensional arrays using:
var arrays = new int[length][,];
Then you need to initialize each array, using either:
arrays[0] = new int[3,3];
Or:
arrays[0] = A1;
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();
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
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();