2D array with two different datatypes in C #
Could you please tell me if it is possible to have a 2D array with 2 day types. I am new to C #.
For example: array[double][string]
I have a radius of colors just with their name like this:
4.7,Iris-setosa
4.6,Iris-setosa
7,Iris-versicolor
6.4,Iris-versicolor
6.9,Iris-versicolor
5.5,Iris-versicolor
6.5,Iris-versicolor
6.3,Iris-virginica
5.8,Iris-virginica
I would like to put them in a 2D array and sort by the first index double
. Please let me know if possible with an example.
source to share
As noted, you probably want to create a simple class for this:
public class Flower {
public double Radius { get; set; }
public string Name { get; set; }
}
var l = new List<Flower>();
l.Add(new Flower() { Radius = 4.7, Name = "Iris-setosa" });
l.Add(new Flower() { Radius = 4.6, Name = "Iris-setosa" });
/* ... */
Flower[] sorted = l.OrderBy(f => f.Radius).ToArray();
You can get away with an array KeyValuePair<int, string>
, but I don't see much reason to go this route unless you're just looking for something quick and dirty.
source to share
The data you are trying to organize looks like a list of name-value pairs, with non-duplicate names. Since the number of elements is different for each name, a 2D array is not an ideal way to model it. You'd be better off with a dictionary that maps names to lists of radii like this:
Dictionary<string,List<decimal>>
This is how your data will be organized in such a dictionary:
var data = new Dictionary<string,List<decimal>> {
{"Iris-setosa", new List<decimal> {4.7M, 4.6M}}
, {"Iris-versicolor", new List<decimal> {7M, 6.4M, 6.9M, 5.5M, 6.5M}}
, {"Iris-virginica", new List<decimal> {6.3M, 5.8M}}
};
I am assuming that the radius representation should be decimal in your case; you could use a different representation of real numbers, for example, float
or double
.
source to share
If you don't want to create a class for whatever reason, you can use Anonymous types as well , so for your case it would be something like:
var a1 = new[] {
new {Radius = 4.7, Name = "Iris-setosa"},
new {Radius = 4.6, Name = "Iris-setosa"}
};
source to share
If it's just for temporary processing, you can use Tuple as well.
List<Tuple<decimal, string>> test = new List<Tuple<decimal, string>>();
test.Add(Tuple.Create<decimal, string>(4.7M, "Iris-setosa"));
test.Add(Tuple.Create<decimal, string>(4.6M, "Iris-setosa"));
var sortedList = test.OrderBy(i => i.Item1);
Don't miss Tuples in your application. Create a class that has the properties you need to store and access.
source to share
Depending on how your application will use this data, a struct (not a class) might also be a possible solution. The structure is passed by value and allocated on the stack instead of the heap. This means that it is not actually garbage collection, but a deallocation when a packet is decayed or when their containing type is deallocated.
Thus, if you have a very small data structure that doesnβt transfer much, the structure may be a more economical choice for you. I think the easiest way to tell is to compare the two alternatives and see which one works best for you.
See additional information:
https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx
source to share