创建一个同时包含整数和日期时间C#的数组

本文关键字:日期 整数 时间 数组 包含 一个 创建 | 更新日期: 2023-09-27 18:24:04

我正试图创建一个打印出来时看起来像这样的数组:

[0]  100   10:00
[1]  60    10:04

我已经创建了数组,现在需要将值添加到数组中:

int[,] ArrayOfSensors = new int[41,41];
/// <summary>
/// Generates Random Blood Pressure Systolic Value
/// </summary>
/// <returns></returns>
public int RandomBPSValue()
{
    Random random = new Random();
    int RandomNumber = random.Next(90, 120);
    for (int runs = 1; runs < 41; runs = runs + 5)
    {
        ArrayOfSensors[runs, DateTime.Now] = RandomNumber;
    }
    return RandomNumber;
}

创建一个同时包含整数和日期时间C#的数组

您可以为代码使用Tuple<int, DateTime>。请在此处阅读MSDN文档。

您的代码看起来与有些相似

var myComplexArray = new Tuple<int, DateTime>[5]; //array of 5 for example
Random random = new Random();
for (int runs = 0; runs < 5; runs++)
{
    int num = random.Next(90, 120); //use your real data here
    myComplexArray[runs] = new Tuple<int, DateTime>(num, DateTime.Now.AddMinutes(runs));
}
//to show your desired output
for (var i = 0; i < myComplexArray.Length; i++)
{
    Console.WriteLine(string.Format("[{0}] {1} {2}", i, myComplexArray[i].Item1, 
                                        myComplexArray[i].Item2.ToShortDateString()));
}

尝试以下操作:

public class Observation
{
    public int Systolic { get; set; }
    public DateTime ObsTime { get; set;}
}
public List<Observation> RandomBPSValue()
{
    List<Observation> myList = new List<Observation>();
    Random random = new Random();
    int RandomNumber = random.Next(90, 120);
    for (int runs = 1; runs < 41; runs = runs + 5)
    {
        Observation observation = new Observation() { ObsTime = DateTime.Now, Systolic = runs};
        myList.Add(observation);
    }
    return myList;
}