how to get length of string [] [] - c#

I have 2D array of string defined as
string[][] input_data;
I can count the number of rows by input_data.GetLength(0)
but if I use input_data.GetLength(1) to get number of column, I always get
System.IndexOutOfRangeException was unhandled
Message=Index was outside the bounds of the array.
Source=mscorlib
StackTrace:
at System.Array.GetLength(Int32 dimension)
...
I also noticed that at debugging, when I hover my mouse on my array (after all the data has been inserted) it only shows the first value (number of row) like this: input_data| {string[18][]}, if I continue expand the array then it shows all the 18 row data with the column data, like this:
How do I get the number of column? (in this case it is 139)

What you posted is a jagged array, not a multi-dimensional array. It's a 1-D array that contains other arrays. There is no second dimension so you can't use data.GetLength(1). Each row can have a different number of columns.
You can get the minimum or maximum number of columns for each row with data.Max(r=>r.Length) or data.Min(r=>r.Length), eg:
var s=new string[][]{
new[] {"a","b"},
new[] {"a"}
};
Console.WriteLine("{0} {1}",s.Max(r=>r.Length),s.Min(r=>r.Length));
will print
2 1
To specify a multi-dimensional array, you need to use the [,] syntax:
var s=new string[,]{
{"a","b"},
{"c","d"},
{"e","f"}
};
Console.WriteLine("{0} {1}",s.GetLength(0),s.GetLength(1));
This will return :
3 2

GetLength(1) would work if you had a multidimensional array:
string[,] input_data = new string[27,139];
var columns = input_data.GetLength(1);
What you have now is a jagged array not a 2D array, so there is no guarantee that all items ('rows') have the same number of elements ('columns').
However in case you can't use multidimensional array for some reason and you are sure that the input will be that way you can use the length of the first element input_data[0].Length

I think you are confusing an array of arrays and a multi dimentional array, run this code :
string[][] array_of_arrays = new string[5][];
Console.WriteLine(array_of_arrays.Length);
array_of_arrays[0] = new string[5];
array_of_arrays[1] = new string[6];
Console.WriteLine(array_of_arrays[0].Length);
Console.WriteLine(array_of_arrays[1].Length);
string[,] multi_d_array = new string[4,2];
Console.WriteLine(multi_d_array.GetLength(0));
Console.WriteLine(multi_d_array.GetLength(1));

This is a jagged array. You can get the first dimensions size as 'input_data.GetLength(0)' and second dimension size as 'input_data.GetLength(1)'. Try it
Example:
string[,] input_data = new string[5,12];
var column1 = input_data.GetLength(0);
var column2 = input_data.GetLength(1);

Related

Get a sub array using array of indexes in one step

In C#, given an array of integers that represents indexes of an array of items, is there a way to get a sub-array of the array of items that correspond to indexes in one step?
int[] indexesArray = {0,2,4,1};
string[] itemsArray = {"hi", "ciao", "yo"," hey","hello"};
string[] result = builtinMagic(itemsArray, indexesArray);
You can simply Select the index from the indexesArray and then get the item at that specific index:
string[] result = indexesArray.Select(idx => itemsArray[idx]).ToArray();

Get one dimensionial array from a mutlidimensional array in c#

I have a two dimensional array like this:
decimal[,] dataArray;
dataArray = new decimal[10, 20];
How can I get an onedimesional array that contains the values from one particular column?
Thanks.
There is no built-in API for slicing multidimensional arrays. Write a loop that goes through all rows, and harvests a specific column into a result array, or use LINQ to "fold" the loop:
var col = Enumerable.Range(0, 20).Select(r=>dataArray[3, r]).ToArray();

Add an item to an already existing 2D integer array in a function in windows form C#

I have a 2D integer array and i want to add an item in it as per the values i get in the method.
private int[,] indexes = new int[100,2];
This is the array declared and below is how i add items in the array as per the indexes but in my method i would not know the exact indexes. Is there a way where in i can get the indexes of the last element in the array and than add an element to it or a way where i can add directly at the end of the existing array
indexes[0,0]= currRowIndex;
indexes[0,1] = 0;
Here i have added at the index 0. Similarly i should be able to add to the last index where the elements in an array ends.
Consider of using nested lists - List<List<int>> From MSDN List
Then new values will be added always to the end of collection
List<List<int>> indexes = new List<List<int>>();
indexes.Add(new List<int> { 1, 2 });
And retrieve value by index
int firstValue = indexes[0][0];
int secondValue = indexes[0][1];
indexes .GetLength(0) -> Gets first dimension size
indexes .GetLength(1) -> Gets second dimension size
so you can add items to the list as
indexes[indexes .GetLength(0),
indexes .GetLength(1)] = value;

How to Convert 2D List to 2D Array in C#

I have the following C# List:
List<Response> listAllData = new List<Response>();
listAllData.Add(new Response() {
strId = propResponse.strId,
strName = propResponse.strName
});
And then I am converting it to an array as such:
object[] array2 = listAllData.ToArray();
But when I'm trying to write data to a range using:
rngValues.Value = array2;
I'm getting an error
Exception from HRESULT: 0x800A03EC
I am (reasonably) certain this is b/c the resulting array2 is not actually a 2D multidimensional array, but instead an array-of-arrays.
So my question is... How do I get my listAllData into a 2D array?
It is not a "jagged array" meaning there are always 2 elements in each entry.
resulting array2 is not actually a 2D multidimensional array, but instead an array-of-arrays.
No, it's a 1-D array of Response objects. If you want it in a 2-D array of objects (where the row is the two string properties from the source object) you'll have to build a loop (Linq does not support 2-D arrays):
object[,] array2 = new object[listAllData.Count,2];
for(int i = 0; i < listAllData.Count; i++)
{
array2[i,0] = listAllData[i].strId;
array2[i,1] = listAllData[i].strName;
}

How to get multidimensional length - of one axis

I have an array such as
string[,] SSISVariableNameValue = new string[,]
{
{"DestinationConfigDB","dest db"},
{"DestinationServer","dest server"},
{"SourceConfigDB","source db"},
{"SourceServer","source server"},
{"SSISConfigFilter","filter"}
};
The .length property is listed as 10. How can i get the 'x' axis of this array..aside from dividing by 2?
An array has a Rank property to tell you the number of dimensions and a GetLength(int d) function to give you the size in a specific dimension. d = 0 .. Rank-1
So you want SSISVariableNameValue.GetLength(0);

Categories