Home > Net >  What container I should use
What container I should use

Time:06-06

I have an array of integers, where consecutive groups of three numbers describe:

  1. Type: capital (1), current (2), or cash (3).
  2. Value of a capital or a current asset, or the exchange rate of a cash asset.
  3. Amount.

Input looks like this: 1 500 10 2 25 100 3 10 50 -1 2 5 -1 Where -1 is termination number to exit loop.

What containter should I use? I was thinking about Dictionary, but it provides only <int,int> type of data. I found out that solution, if there is only groups of two numbers.

for (int i = 0; i < numbers.Length; i  = 2)
        {  int key = numbers[i];

            if (key == -1)
                break;
            int priceKey = numbers[i   1];
            totals.Select(e.Value).Append(-1).ToArray();

I know that in C there is std::pair which fixes my problem, but I dont know hot wo use Pair in dictionary. Any help with synthax how to use in my problem?

CodePudding user response:

This is why we have classes/interfaces/types in programming languages.

public enum PaymentType {
    Capital, Current, Cash
}

public class Payment {
    public PaymentType Type {get; set;}
    public int Value {get; set;}
    public int Amount {get; set;}
}

// now we can use it and any programmer will easier understand what we are trying to accomplish
var payments = new List<Payment>();
payments.Add(new Payment(PaymentType.Capital, 9, 5))
...

// we can also use dictionaries if we want to 
Dictionary<string, Payment> payments = new Dictionary<string, Payment>()
...

CodePudding user response:

If you really don't want to define a class for this, C# also has Tuples. That would let you have the sample data from the question in an array like this:

var data = new (int, int, int)[]{(1, 500, 10), (2, 25, 100), (3, 10, 50), (1, 2, 5)};

or in a list:

var data = new List<(int, int, int)> {(1, 500, 10), (2, 25, 100), (3, 10, 50), (1, 2, 5)};
  • Related