string[][] myArray = new[size][];
for(int i=0;i<2;i++){
myArray[i][0] = newValue.toString();
}
While assigning the values i get an error
I get the Object Reference not set to instance of object exception. Please help
You have created a jagged array of strings. The outermost array has been initialized to size elements, but that means there are size spaces each for a string[], each of which is currently null. You need to create the inner arrays, or create a rectangular array.
Based on the poor code, it should be something like this:
string[][] myArray = new string[size][];
for (int j = 0; j < myArray.Length; j++) {
myArray[j] = new string[3];
for (int i = 0; i < myArray[j].Length; i++) {
myArray[j][i] = newValue.ToString();
}
}
Try this
int dim1 = 2;
int dim2 = 1;
string[,] iii = new string[dim1, dim2];
for (int i = 0; i < iii.GetLength(0); i++)
{
iii[i, 0] = "myValue";
}
Related
Basically, I am trying to split an array and want to pass its value into another array.
But, I am not able to do it.It is givin an error:
Cannot implicitly convert type String[] to string"
StreamReader EmployeeFile = new StreamReader(#"C:\Users\user\Desktop\FoodDeliverySystem\Employee_details.txt");
String ReadEmployeeData = EmployeeFile.ReadToEnd();
String[] ReadEmployeeDataByLine = ReadEmployeeData.Split(';');
for (int k = 0; k < 5; k++)
{
for (int l = 0; l < 5; l++)
{
Console.WriteLine("test");
String[,] ReadEmployeeDataByLineByCategorie = new string[k, l];
ReadEmployeeDataByLineByCategorie[k,l] = ReadEmployeeDataByLine[l].Split(',');
}
}
Since you can't be sure of how many of those values you're gonna have in each category of yours, you should use jagged arrays
That should do:
var readEmployeeDataByLine = new StreamReader(#"C:\pathToFile.txt").ReadToEnd().Split(';');
var readEmployeeDataByLineByCategorie = new string[readEmployeeDataByLine.Length][];
for (int i = 0; i < readEmployeeDataByLineByCategorie.Length; i++)
readEmployeeDataByLineByCategorie[i] = readEmployeeDataByLine[i].Split(',');
ReadEmployeeDataByLineByCategorie[k,l] is a string
while ReadEmployeeDataByLine[l].Split(',') is a string[]
string[] ReadEmployeeDataByLine = ReadEmployeeData.Split(';');
for(int i = 0;i< ReadEmployeeDataByLine.Length;i++)
{
string split = ReadEmployeeDataByLine[l].Split(',');
for(int j=0; j<split.Length;j++)
ReadEmployeeDataByLineByCategorie[i,j] = split[j]
}
I have a method which returns the following type and object:
object {object[,]}
test {object[19, 2]}
Is it possible to convert this into 2d string array, or a datatable?
Looks akward, but I haven't found another way.
int firstDimensionSize = 19;
int secondDimensionSize = 2;
object[,] objectArray = new object[firstDimensionSize, secondDimensionSize];
string[,] stringArray = new string[objectArray.Length / secondDimensionSize, secondDimensionSize];
for (int i = 0; i < objectArray.Length / secondDimensionSize; i++)
{
for (int j = 0; j < secondDimensionSize; j++)
{
stringArray[i, j] = objectArray[i, j].ToString();
}
}
In case your second dimension is always the same you could leave out the inner loop, replacing it with static code.
See also https://www.dotnetperls.com/2d
I made this extension method, but you can aswell make it a normal method if you don't need it that often.
public static class Extensions
{
public static string[,] ConvertToString2D(this object[,] arr)
{
int dim1 = arr.GetLength(0);
int dim2 = arr.GetLength(1);
string[,] strArray = new string[dim1, dim2];
for (int i = 0; i < dim1; i++)
{
for (int j = 0; j < dim2; j++)
{
strArray[i, j] = Convert.ToString(arr[i, j]);
}
}
return strArray;
}
}
and then you can call it like this:
var result = objArr.ConvertToString2D();
I initialized an object:
object[,] A = new object[range.RowCount() + 1, range.ColumnCount() + 1];
After filling this I need to iterate all elements of object in loop.
How to iterate this in for loop?
I tried to do: for(var i = 0; i < A.Count(); i++){}
But there is not property Count() for object, also this is matrix.
For things like this you might want nested loops, like so:
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
//do your magic
}
}
Arrays have Length property:
for (var i = 0; i < A.Length; i++)
{
var b = A[i, 1];
}
Count() is a method of IEnumerable
This is a simplified version of the problem im trying to solve. Im trying to add an int[] to a List, but it updates all the arrays in the List with the one from the last iteration. Why is that? And how does I solve this problem? This isn't a problem if it's just 1 int for example.
intArray = new int[9];
for (int i = 0; i < 9; i++)
{
intArray[i] = i;
}
Test.Add(intArray);
for (int i = 0; i < 9; i++)
{
intArray[i] = i * 2;
}
Test.Add(intArray);
foreach (var item in Test)
{
for (int i = 0; i < 9; i++)
{
Console.WriteLine(item[i]);
}
}
Console.ReadKey();
}
public static int[] intArray { get; set; }
public static List<int[]> Test = new List<int[]>();
Using new keyword you create intArray only once. Then there is only one reference. After that you add intArray reference to collection List for multiple times.
Try this,
intArray = new int[9];
for (int i = 0; i < 9; i++)
{
intArray[i] = i;
}
Test.Add(intArray);
intArray = new int[9]; // create new intArray here
for (int i = 0; i < 9; i++)
{
intArray[i] = i * 2;
}
Test.Add(intArray);
after u added your intArray object into your arraylist ,simply create another object of that
intArray = new int[length];
hope it will works
I'm getting "reference to an object was not set for an instance of the object" run-time error with the suggestion: "Use the new keyword to create an object instance.
But I don't understand what I'm doing wrong.
There is my code:
int numListas = 10;
x = 15;
int[][] listas;
listas = new int[numListas][];
Random random = new Random(); // generate random number
for (idx = 0; idx < numListas; idx++)
{
int idx_el;
for (idx_el = 0; idx_el < x ; idx_el++)
{
listas[idx][idx_el] = random.Next(0,100);
}
}
This line:
int[][] listas;
declares a variable which is a reference to an array of arrays.
This line:
listas = new int[numListas][];
initializes the variable to refer to an array of numListas array references... but each of those "subarray" references is null to start with. You need to initialize each "subarray", probably in your outer loop:
for (idx = 0; idx < numListas; idx++)
{
listas[idx] = new int[x];
...
}
Note that an alternative is to use a rectangular array instead of an array of arrays:
int[,] listas = new int[numListas, x];
...
// In your loop
listas[idx, idx_el] = random.Next(0, 100);
As an aside, code generally reads more clearly when you declare and initialize variables together, rather than declaring them earlier and then initializing them later. Also, underscores in C# variable names aren't terribly idiomatic, and the overall variable names aren't terribly clear. I'd write your code as:
// Consider declaring these as constants outside the method
int rowCount = 10;
int columnCount = 15;
int[][] values = new int[rowCount];
Random random = new Random();
for (int row = 0; row < rowCount; row++)
{
values[row] = new int[columnCount];
for (int column = 0; column < columnCount; column++)
{
values[row][column] = random.Next(100);
}
}
You should also consider initializing a single Random instance outside the method and passing it in - see my article on Random for more information.
You need to initialize each inner array within your jagged array:
int numListas = 10;
x = 15;
int[][] listas;
listas = new int[numListas][];
Random random = new Random(); // generate random number
for (idx = 0; idx < numListas; idx++)
{
listas[idx] = new int[x]; // initialize inner array
int idx_el;
for (idx_el = 0; idx_el < x ; idx_el++)
{
listas[idx][idx_el] = random.Next(0,100);
}
}
Alternatively, you may use a two-dimensional array:
int numListas = 10;
x = 15;
int[,] listas;
listas = new int[numListas,x]; // 2D array
Random random = new Random(); // generate random number
for (idx = 0; idx < numListas; idx++)
{
int idx_el;
for (idx_el = 0; idx_el < x ; idx_el++)
{
listas[idx,idx_el] = random.Next(0,100);
}
}
You don't set a dimension for the second dimension
listas = new int[numListas][];.
As you are using x as the limit of your loop, use that as dimension:
for (idx = 0; idx < numListas; idx++)
{
int idx_el;
listas[idx] = new int[x]; // initialize the second dimension
for (idx_el = 0; idx_el < x ; idx_el++)
{
listas[idx][idx_el] = random.Next(0,100);
}
}
Alternatively, you can use a rectangular array at the beginning:
int[,] listas = new int[numListas,x];
Did you forget to initialize the second dimension of the array?
listas = new int[numListas][x];
initialize this ^