Initialize array of arrays with a set array length - c#

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();

Related

C# - float[][] declaration

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];

How to add data from 1d to 2d array in C#

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,

two dimension array; one dimension length is defined one is undefined in c#

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

Can an array reference 2 different arrays

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]);

Reversing order of elements in multidimensional array

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}

Categories

Resources