How can I make array in existing array's index without using pointer for e.g
float[] currentNode = new float[12]
float[] neighbour = new float[12]
neighbour[8] = new float[12]
neighbour[8] = currentNode;
and can access with neighbour[8][1]
other option is something using pointers.
float *pointer;
int []array = new int[12];
pointer = &array[0];
neighbour[8] = pointer
so does first solution possible w/o changing my both arrays ? any other solutions ?? .
You are looking for Multidimensional arrays.
int[,] myArray;
myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}}; // OK
You can't do that.
You have an array of float values, not an array of arrays.
Which means you cannot assign one element in the array (which holds a float value) a value which is an array.
You'd have to redefine your variables as:
float[][] neighbour = new float[12][];
This will declare an array of arrays, which means each element of the neighbour array can hold arrays of different lengths, or no array (null-reference).
If you want to declare a matrix, you can do it like this:
float[,] neighbour = new float[12, 8];
You also could use generics:
List<List<float>> numbers = new List<List<float>>();
numbers.Add(new List<float>());
numbers.Add(new List<float>());
numbers[0].Add(2.3);
numbers[0].Add(5);
numbers[0].Add(8.74);
numbers[1].Add(6.8);
numbers[1].Add(9.87);
float aNumber = numbers[1][0]; //6.8
float anotherNumber = numbers[0][2]; //8.74
Related
I'm trying to create a empty array of points, for this i'm using the following
Point[] listapontos = new[]; //should create a empty point array for later use but doesn't.
Point ponto = new Point(); //a no coordinates point for later use
for (int i = 1; i <= 100; i++) //cycle that would create 100 points, with X and Y coordinates
{
ponto.X = i * 2 + 20;
ponto.Y = 20;
listapontos[i] = ponto;
}
I'm having some trouble, because I can't create a empty array of points. I could create a empty array of strings using a list, but since i will need two elements, a list isn't useful here.
Any hints? (hints for the problem are also welcome)
// should create a empty point array for later use but doesn't.
No, what you've specified just isn't valid syntax. If you want an empty array, you could use any of:
Point[] listapontos = new Point[0];
Point[] listapontos = new Point[] {};
Point[] listapontos = {};
However, then you've got an array with 0 elements, so this statement:
listapontos[i] = ponto;
... will then throw an exception. It sounds like you should either use a List<Point>, or create an array of size 101:
Point[] listapontos = new Point[101];
(Or create an array of size 100, and change the indexes you use - currently you're not assigning anything to index 0.)
It's important to understand that in .NET, an array object doesn't change its size after creation. That's why it's often convenient to use List<T> instead, which wraps an array, "resizing" (by creating a new array and copying values) when it needs to.
I could create a empty array of strings using a list, but since i will
need two elements, a list isn't useful here.
You can define a class like this:
public class Point
{
public double X {get;set;}
public double Y {get;set;}
}
Then you can use a List<Point>:
List<Point> points = new List<Point>();
points.Add(new Point(){X=10, Y=20});
The reason this doesn't work is 2-fold.
Firstly, when you say
Point[] listapontos = new[];
you are using invalid syntax. It should be more like
Point[] listapontos = new Point[100];
Now, secondly, when you are writing into the array, you are never creating a new point.
Point is a class, which means it is passed by reference. This means that whenever you are writing Ponto's address in, but not a new Ponto.
Instead what you should do is something more like
for(int i = 0; i < 100; i++)
{
listapontos[i] = new Point(i * 2 + 20, 20);
}
By using the new keyword, you are creating a new point in memory and storing that point's address in the array, instead of writing the address to the same point 100 times into the array.
I have created a class with a 2d integer array as a member. What I want, is for that member to be dynamically allocated as an x by x matrix when I instantiate a truck instance through the constructor. Here is my code:
public class truck
{
public int[][] mat;
truck(int x)
{
mat = new int[x][x];
for(int i=0;i<x;i++)
for(int j=0;j<x;j++)
mat[i][j]=0;
}
}
It is giving me the following error:
Invalid rank specifier: expected ',' or ']'
on this line: mat = new int[x][x];
Why is it giving me this error?
You can't initialize a jagged array like that. You have to initialize the first dimension first, and then initialize the elements:
mat = new int[x][];
//now you can do this:
mat[0] = new int[x];
...
Maybe you want a multidimensional array instead?
public int[,] mat;
...
mat = new int[x,x];
By using [][] you create an array of arrays. It means that each element of the first dimension contains an array.
That is why you need to initialize x arrays instead of just one.
You can do that by:
for (int i=0; i<x; i++) mat[i] = new int[x];
If you need to create just a two dimensional matrix, you can use int[,] instead of int[][]. In this case you would be able to initialize it like mat = new int[x,x]
I thought searching SO would show me a hit regarding 2D lists to 2D arrays but it seems it's not as common as I had thought.
This is what I've got:
// init array
int[][] a = new int[10][10];
// change 2D array to 2D list
List<List<int>> b = a.Cast<List<int>>().ToList();
// change 2D list back to 2D array
???
How can I change b back to a 2D array? Also is the above correct?
Something like this:
List<List<int>> lists = arrays.Select(inner=>inner.ToList()).ToList();
int[][] arrays = lists.Select(inner=>inner.ToArray()).ToArray();
It's totally wrong. You can't get b in that way. And even the initialization is wrong. And in .NET there are two types of multidimensional arrays... True multidimensional arrays and jagged arrays...
Let's start... You are using a jagged array (I won't tell you what it's, or the difference, you didn't ask for it... if you need them, google for it)
int[][] a = new int[10][]; // see how you define it?
// Only the first dimension can be is sized here.
for (int i = 0; i < a.Length; i++)
{
// For each element, create a subarray
// (each element is a row in a 2d array, so this is a row of 10 columns)
a[i] = new int[10];
}
Now you have defined a 10x10 array jagged array.
Now a little LINQ:
You want a list:
List<List<int>> b = a.Select(row => row.ToList()).ToList();
you want back an array:
int[][] c = b.Select(row => row.ToArray()).ToArray();
The first expression means
foreach element of a, we call this element row `a.Select(row =>` <br>
make of this element a List `row.ToList()` and return it<br>
of all the results of all the elements of a, make a List `.ToList();`
The second is specular.
Now... Just out of curiosity, and if you had a true multidimensional array? then it was complex, very comples.
int[,] a = new int[10,10];
int l0 = a.GetLength(0);
int l1 = a.GetLength(1);
var b = new List<List<int>>(
Enumerable.Range(0, l0)
.Select(p => new List<int>(
Enumerable.Range(0, l1)
.Select(q => a[p, q]))));
var c = new int[b.Count, b[0].Count];
for (int i = 0; i < b.Count; i++)
{
for (int j = 0; j < b[i].Count; j++)
{
c[i, j] = b[i][j];
}
}
With a tricky (and horrible) LINQ expression we can "convert" a multidimensional array to a List<List<int>>. The road back isn't easily doable with LINQ (unless you want to use the List<T>.ForEach() that you shouldn't ever use, because it's not kosher, and then List<T>.ForEach() isn't LINQ)... But it's easily doable with two nested for ()
This question already has answers here:
Differences between a multidimensional array "[,]" and an array of arrays "[][]" in C#?
(12 answers)
Why we have both jagged array and multidimensional array?
(13 answers)
Closed 9 years ago.
I confuse between double[][] and double[,] in C#.
My teammate give me a function like this:
public double[][] Do_Something(double[][] A)
{
.......
}
I want to use this function:
double[,] data = survey.GetSurveyData(); //Get data
double[,] inrma = Do_Something(data);
It lead an error: invalid argument.
I don't want to edit my teammate's code.
Does it have any way to convert double[][] to double [,] ?
Thanks!
A double[][] is an array of double[] (An array of arrays)
but double[,] is a single 2 dimensional double array
Example :
double[] Array1 = new double[] {1,2,3};
double[] Array2 = new double[] {4,5,6};
double[][] ArrayOfArrays = new double[][] {Array1,Array2};
double[,] MultidimensionalArray = new double[,] {{1,2}, {3,4}, {5,6}, {7,8}};
double[][] and double[,] have different meanings.
double[][] is jagged, so some elements can be of different lengths than others.
double[,] is "rectangular", so all elements are of the same length.
You could write a method to "convert" between the two, but how will you rectify the differences? I.e. how will you decide to "trim" from the long elements or expand the short elements in order to make it rectangular?
static T[,] Convert<T>(T[][] array)
{
if (array == null)
throw new ArgumentNullException("array");
if (array.Length == 0)
return new T[0, 0];
T[,] retval = new T[array.Length, array[0].Length];
for (int i = 0; i < array.Length; i++)
for (int j = 0; j < array[i].Length; j++)
if (array[i].Length != retval.GetLength(1))
throw new Exception();
else
retval[i, j] = array[i][j];
return retval;
}
They are two different types of arrays.
double[][] is a jagged array, which is an array of arrays; each of these arrays can have a different length, which leads to a problem.
double[,] is just a multidimensional array. Each row will have an equal number of columns and each column will have an equal number of rows.
This size difference causes a problem since the jagged array could really be different dimensions for different rows. You could write a method to convert between the two if you knew the exact dimensions of the jagged array, but in that case I would suggest rewriting the original method to accept and return a multidimensional array (double[,]).
I've got a two-dimensional array like this:
double[,] results = new double[100,100];
I'd like to pass every one dimensional part of the array to a function as a paremeter.
for (int i = 0; i < 100; i++){
cool_function (results[???], 10);
}
How do I do this in C#?
source
Jagged arrays are arrays of arrays. The elements of a jagged array are other arrays.
Declaring Jagged Arrays
Declaration of a jagged array involves two brackets. For example, the following code snippet declares a jagged array that has three items of an array.
int[][] intJaggedArray = new int[3][];
The following code snippet declares a jagged array that has two items of an array.
string[][] stringJaggedArray = new string[2][];
Initializing Jagged Arrays
Before a jagged array can be used, its items must be initialized. The following code snippet initializes a jagged array; the first item with an array of integers that has two integers, second item with an array of integers that has 4 integers, and a third item with an array of integers that has 6 integers.
// Initializing jagged arrays
intJaggedArray[0] = new int[2];
intJaggedArray[1] = new int[4];
intJaggedArray[2] = new int[6];
We can also initialize a jagged array's items by providing the values of the array's items. The following code snippet initializes item an array's items directly during the declaration.
// Initializing jagged arrays
intJaggedArray[0] = new int[2]{2, 12};
intJaggedArray[1] = new int[4]{4, 14, 24, 34};
intJaggedArray[2] = new int[6] {6, 16, 26, 36, 46, 56 };
You can't do it without copying the corresponding part of the array.
Otherwise, you can use a double[][]. To initialize:
double[][] results = new double[100][];
for(int i = 0; i < 100; i++)
results[i] = new double[100];
You could do this by using a jagged array Type[][] instead of Type[,]. In this case you can just pass array[index]. Otherwise you will have to either pass the two-dimensional array together with the index of the subarray of interest and perform the indexing in the called method or create a copy of the subarray of interest.
If you mean for an array myarray[x][y] that you want to call a function for x arrays of size y then all you need is the following code:
int i;
for (i = 0; i < 100; i++)
{
cool_function(array[i], 10);
}