C#访问浮动的数组列表
我试图解决一个小问题: 想象一下每月每天有1440次温度测量 “国际日[31]浮动温度[1440]” Microsoft文档和我找到的所有其他文档有如下内容:C#访问浮动的数组列表,c#,arraylist,C#,Arraylist,我试图解决一个小问题: 想象一下每月每天有1440次温度测量 “国际日[31]浮动温度[1440]” Microsoft文档和我找到的所有其他文档有如下内容: float[][] jaggedArray = { new float[] { 88.3F, 33.9F, 55.4F, 99.9F }, new float[] { 1.1F, 3F, 5F, 9.9F }, new float[] { 1F, 3F, 5F, 9F } }; 现在我可以使用 float
float[][] jaggedArray =
{
new float[] { 88.3F, 33.9F, 55.4F, 99.9F },
new float[] { 1.1F, 3F, 5F, 9.9F },
new float[] { 1F, 3F, 5F, 9F }
};
现在我可以使用
float result = jaggedArray[2][1];
到目前为止还不错
缺点是我必须调整数组的大小并初始化它,然后才能开始
数据集的大小可能不同。(并非每个月都有30天)
所以我认为使用Arraylist中的Arraylist是个好主意
ArrayList valueSet1= new ArrayList();
ArrayList valueSet2 = new ArrayList();
ArrayList Sets = new ArrayList();
valueSet1.Add(1.0F);
valueSet1.Add(2.0F);
valueSet1.Add(3.0F);
valueSet1.Add(4.0F);
valueSet2.Add(5.0F);
valueSet2.Add(6.0F);
valueSet2.Add(7.0F);
valueSet2.Add(8.0F);
Sets.Add(valueSet1);
Sets.Add(valueSet2);
这同样有效,在“监视”窗口中,它看起来与数组解决方案几乎相同,只是访问值不起作用:
float test= Sets[1][1]; //does not work
我还测试了foreach方法,但没有成功:
foreach (var ValueSet in Sets)
{
// ValueSet exists here but:
foreach (var item in ValueSet) // does not recognize ValueSet
{
}
}
我现在被难住了
谢谢您的帮助。正如其他人提到的,现在有比ArrayList更好的选项 但是,如果您仍想继续,您可能希望这样做:
float x = (float)((ArrayList)Sets[1])[1];
或
ArrayList中有“对象”,您需要显式地转换它们。如果我要这样做,我会采取完全不同的策略。我会有一个代表一天测量值的课程。然后,我会有一个类,它表示一个月的每日度量值——它将包含一个每日度量实例的字典,按月份的日期进行索引 所以,做一些家务来开始工作。我喜欢象征性地工作
public enum Months
{
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
现在我可以用Months.mummer
而不是9
现在,我将创建一个跟踪白天温度测量的工具。我将给每个度量加上时间戳(使用TimeSpan
)。我将在字典中跟踪测量值,根据记录的时间跟踪每个测量值。首先是简单的部分:
public class DailyTemperatureMeasurements : IEnumerable<(TimeSpan time, float temperature)>, IEnumerable<float>
{
private readonly Dictionary<TimeSpan, float> _measurements = new Dictionary<TimeSpan, float>();
private IEnumerable<float> _enumerableImplementation;
public void Add(TimeSpan time, float temperature)
{
_measurements.Add(time, temperature);
}
IEnumerator<float> IEnumerable<float>.GetEnumerator()
{
foreach (var measurement in _measurements)
{
yield return measurement.Value;
}
}
public IEnumerator<(TimeSpan time, float temperature)> GetEnumerator()
{
foreach (var measurement in _measurements)
{
yield return (measurement.Key, measurement.Value);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<float>) this).GetEnumerator();
}
}
现在我跟踪了一天的所有测量结果。让我们跟踪他们一个月:
正如你所指出的,并非所有月份都有31天。事情变得更复杂了(二月)。我们不想要坏数据,所以我有两个小的私人工作者,我要加入我的每月测量课程:
private static readonly Dictionary<Months, int> MonthLengths = new Dictionary<Months, int>
{
{Months.January, 31},
{Months.February, 28},
{Months.March, 31},
{Months.April, 30},
{Months.May, 31},
{Months.June, 30},
{Months.July, 31},
{Months.August, 31},
{Months.September, 30},
{Months.October, 31},
{Months.November, 30},
{Months.December, 30},
};
private static int GetMonthLength(Months month, int year)
{
if (month != Months.February)
{
return MonthLengths[month];
}
//otherwise, month is February, so...
if (year % 400 == 0)
{
return 29;
}
if (year % 100 == 0)
{
return 28;
}
if (year % 4 == 0)
{
return 29;
}
return 28;
}
private static readonly Dictionary monthlength=新字典
{
{月,1月31日},
{月,2月28日},
{月,3月31日},
{月,4月30日},
{月,5月31日},
{Months.June,30},
{月,7月31日},
{月,8月31日},
{月,9月30日},
{月,10月31日},
{月,11月30日},
{月,12月30日},
};
私有静态整数GetMonthLength(月,整数年)
{
如果(月!=月。二月)
{
返回月长[月];
}
//否则,月份是二月,所以。。。
如果(年份%400==0)
{
返回29;
}
如果(年份%100==0)
{
返回28;
}
如果(第%4年==0)
{
返回29;
}
返回28;
}
这样,如果有人试图添加2100年4月31日或2月29日的数据,我可以抛出一个异常
跟踪每月测量值实际上非常简单:
public class TemperatureMeasurements
{
private readonly Dictionary<int, DailyTemperatureMeasurements> _dailyMeasurements = new Dictionary<int, DailyTemperatureMeasurements>();
public TemperatureMeasurements(int year, Months month)
{
//you may want to sanity check the year value (no years less than now, no years greater than 2500) - maybe
Year = year;
Month = month;
}
public int Year { get; }
public Months Month { get; }
public void Add(int day, TimeSpan time, float measurement)
{
if (day < 1 || day > GetMonthLength(Month, Year))
{
throw new ArgumentOutOfRangeException(nameof (day), "Day value must be within range for month: {_month}");
}
if (!_dailyMeasurements.ContainsKey(day))
{
_dailyMeasurements.Add(day, new DailyTemperatureMeasurements());
}
_dailyMeasurements[day].Add(time, measurement);
}
public void Add(TimeSpan time, float measurement)
{
var now = DateTime.Now;
var monthToday = (Months)now.Month;
if (monthToday != Month)
{
throw new ArgumentException($"This overload of {nameof(Add)} can only be used with today's date is within the month associated with this {nameof(TemperatureMeasurements)} instance ");
}
var dayToday = now.Day;
Add(dayToday, time, measurement);
}
public void Add(float measurement)
{
var now = DateTime.Now.TimeOfDay;
Add(now, measurement);
}
public float GetMeasurementAt(int day, TimeSpan time)
{
if (!_dailyMeasurements.ContainsKey(day))
{
throw new ArgumentOutOfRangeException(nameof(day), "No measurements for {Month} {day} have been recorded");
}
return _dailyMeasurements[day].GetMeasurementAt(time);
}
//This is where that MonthLengths dictionary and GetMonthLengths function go
}
公共类温度测量
{
专用只读词典_dailymasurements=新词典();
公共温度测量(国际年,月)
{
//您可能需要检查年份值(不少于现在的年份,不超过2500年)-可能
年=年;
月=月;
}
公共整数年{get;}
公共月份月份{get;}
公共无效添加(整数天、时间跨度时间、浮动度量)
{
如果(日<1 | |日>月长(月,年))
{
抛出新ArgumentOutOfRangeException(nameof(day),“day值必须在月份的范围内:{u month}”);
}
如果(!_dailymasurements.ContainsKey(天))
{
_dailyMeasurements.添加(天,新的DailyTemperatureMeasurements());
}
_每日测量[天]。添加(时间,测量);
}
公共无效添加(时间跨度时间、浮动测量)
{
var now=DateTime.now;
var monthToday=(月)now.Month;
如果(月日!=月)
{
抛出新的ArgumentException($“此{nameof(Add)}重载只能用于与此{nameof(TemperatureMeasurements)}实例关联的月份内的今天的日期”);
}
var dayToday=now.Day;
添加(今天、时间、测量);
}
公共空间添加(浮动测量)
{
var now=DateTime.now.TimeOfDay;
添加(现在,测量);
}
公共浮点GetMeasureStat(整数天,TimeSpan时间)
{
如果(!_dailymasurements.ContainsKey(天))
{
抛出新ArgumentOutOfRangeException(nameof(day),“没有记录{Month}{day}的度量值”);
}
返回_dailymasurements[day].getMeasurementStat(time);
}
//这就是MonthLength字典和GetMonthLength函数的作用
}
您需要提供其中一个将用于的月份和年份(以便可以正确计算一个月的天数)
数据保存在一个字典中,其中键是一个月的日期(1..[该月的天数]),值是一个DailyTemperatureMeasurements
(我们之前创建的类)实例
您可以通过多种方式添加数据:
- 您可以指定月份的日期、时间和度量值
void Add(整数天、时间跨度时间、浮动测量)
- 您可以只指定时间和测量值(假设您指的是今天)
void Add(时间跨度时间,浮动测量)
- 您只需指定度量(假设您指的是今天和现在的日期和时间)
void Add(浮动测量)
要获取一个值,您可以调用getmeasurementtat
,传递da
private static readonly Dictionary<Months, int> MonthLengths = new Dictionary<Months, int>
{
{Months.January, 31},
{Months.February, 28},
{Months.March, 31},
{Months.April, 30},
{Months.May, 31},
{Months.June, 30},
{Months.July, 31},
{Months.August, 31},
{Months.September, 30},
{Months.October, 31},
{Months.November, 30},
{Months.December, 30},
};
private static int GetMonthLength(Months month, int year)
{
if (month != Months.February)
{
return MonthLengths[month];
}
//otherwise, month is February, so...
if (year % 400 == 0)
{
return 29;
}
if (year % 100 == 0)
{
return 28;
}
if (year % 4 == 0)
{
return 29;
}
return 28;
}
public class TemperatureMeasurements
{
private readonly Dictionary<int, DailyTemperatureMeasurements> _dailyMeasurements = new Dictionary<int, DailyTemperatureMeasurements>();
public TemperatureMeasurements(int year, Months month)
{
//you may want to sanity check the year value (no years less than now, no years greater than 2500) - maybe
Year = year;
Month = month;
}
public int Year { get; }
public Months Month { get; }
public void Add(int day, TimeSpan time, float measurement)
{
if (day < 1 || day > GetMonthLength(Month, Year))
{
throw new ArgumentOutOfRangeException(nameof (day), "Day value must be within range for month: {_month}");
}
if (!_dailyMeasurements.ContainsKey(day))
{
_dailyMeasurements.Add(day, new DailyTemperatureMeasurements());
}
_dailyMeasurements[day].Add(time, measurement);
}
public void Add(TimeSpan time, float measurement)
{
var now = DateTime.Now;
var monthToday = (Months)now.Month;
if (monthToday != Month)
{
throw new ArgumentException($"This overload of {nameof(Add)} can only be used with today's date is within the month associated with this {nameof(TemperatureMeasurements)} instance ");
}
var dayToday = now.Day;
Add(dayToday, time, measurement);
}
public void Add(float measurement)
{
var now = DateTime.Now.TimeOfDay;
Add(now, measurement);
}
public float GetMeasurementAt(int day, TimeSpan time)
{
if (!_dailyMeasurements.ContainsKey(day))
{
throw new ArgumentOutOfRangeException(nameof(day), "No measurements for {Month} {day} have been recorded");
}
return _dailyMeasurements[day].GetMeasurementAt(time);
}
//This is where that MonthLengths dictionary and GetMonthLengths function go
}