3

Can you please tell me if it is possible to have a 2D array with two day types. I am new to C#.

For example: array[double][string]

I have radius of flowers alone with their name as follows:

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 these in to a 2D array and sort it according to the first double index. Please let me know if this is possible with an example.

D P.
  • 1,039
  • 7
  • 27
  • 56
  • 6
    You don't want that. You want a class that describes the overall object, with properties describing each piece of data the object contains. Then you might have an array or list of that class. – Anthony Pegram Oct 03 '14 at 16:21
  • Or a `Dictionary` but the object is better for solving your problem. – BradleyDotNET Oct 03 '14 at 16:22
  • @AnthonyPegram has it right. Create a class and add properties like radius and name. Then instanciate and populate them in a list. Ordering will be a simple linq expression. – crthompson Oct 03 '14 at 16:23
  • Thank you for all of your answers and comments. They were all very helpful. – D P. Oct 03 '14 at 17:26

5 Answers5

3

The data that you are trying to organize looks like a list of name-value pairs, with non-unique names. Since the number of items is different for each name, 2D array is not the ideal way to model it. You would be better off with a dictionary that maps names to lists of radii as follows:

Dictionary<string,List<decimal>>

Here is how your data would 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 representation of the radius needs to be decimal in your case; you could use another representation of real numbers, too, such as float or double.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • You'd need to flatten that dictionary to meet the order by radius requirement. Doable, but not trivial. Not saying it doesn't necessarily fit the problem domain, but doesn't seem a great choice given the lone requirement to sort by radius. – Mark Brackett Oct 09 '14 at 00:51
2

As comments have said, you likely want 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 could get away with an array of KeyValuePair<int, string>, but I don't see much reason to go that route unless you're just looking for something quick and dirty.

Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
  • @Brackett Thank you for showing me with an example. So, if I wanted to print out the result of these values, how would I do it? For example, if I want to access the diameter result of the 3rd element `Console.WriteLine(l[0,2]);` – D P. Oct 04 '14 at 19:56
  • 1
    @DP. `l` has one dimension, you cannot index it with two indexes. Radius of the third element would be `l[2].Radius` (indexes are zero-based). – Sergey Kalinichenko Oct 04 '14 at 20:53
2

If you don't want to create a class for some reason you can also use Anonymous Types, so for your case it will be something like:

        var a1 = new[] {
            new {Radius = 4.7, Name = "Iris-setosa"},
            new {Radius = 4.6, Name = "Iris-setosa"}
        };
Sergey Malyutin
  • 1,484
  • 2
  • 20
  • 44
0

If this is just for temporary processing, you can also use a Tuple.

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 pass Tuples around in your application though. Create a class that has the properties on it that you need to store and access.

DVK
  • 2,726
  • 1
  • 17
  • 20
0

Depending on how your application will use this data, a struct (instead of a class) may also be a possible solution. The struct is passed by value and is allocated on the stack, instead of the heap. This means it is not really garbage collected but deallocated when the stack unwinds or when their containing type gets deallocated.

So, if you have a very small data structure that is not passed around much a struct may be a more memory efficient choice for you. I think the easiest way to tell is to benchmark the two alternatives and see which one works best for you.

See for further reference:

https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx

Why is 16 byte the recommended size for struct in C#?

Community
  • 1
  • 1
Jake Drew
  • 2,230
  • 23
  • 29