表示 3 个值的集合的最佳方式
Best way to represent sets of 3 values
假设我有 100 组 3 个值。第一个值是时间,第二个值是 x 位置,第三个值是 y 位置。例如,这将是一个点在 100 秒内的 xy 位置。
我有 PHP 背景。在 PHP 中,我会使用这样的 3D 关联数组来完成它:
position["time"]["x"]["y"]
所以我可以在特定时间访问 x,y 值。你将如何在 C# 中执行此操作?我认为像 list 和 dictionary 这样的泛型可以做到这一点。但我不知道如何为 3D 数据集实现键值。
在 C# 中有类型概念,您需要创建一个具体的自定义类型 struct(值类型)或 class(引用类型)
你可以创建一个新类型,在这种情况下我正在创建 class 就像:
public class Position
{
public DateTime Time {get;set;}
public int X {get;set;}
public int Y {get;set;}
}
现在在某个时候你会有一个 class 位置的实例:
Position postion = new Position(); // object created
positon.Time = DateTime.Now;
position.X = 1;
position.Y =0;
以及从中获取值的相同方式:
DateTime time = position.Time;
int positionX = position.X;
int positionY = position.Y;
我建议阅读 from MSDN here
类型的基础知识
对于集合部分,我们有 Framework 提供的 C# 集合,其中包括 Array,List,可用于保存 Position
的多个对象。
希望对您有所帮助!
一个简单的解决方案可能是使用元组,如果这适合您的需要,例如
Example:
var tuple = Tuple.Create(someBytes, moreBytes, myString);
您可以使用 tuple.Item1、tuple.Item2 和 tuple.Item3 访问这些值。
如果需要,当然也可以使用多个元组,例如
例子
var list = new List<Tuple<byte, byte, string>> {
tuple,
anotherTuple,
thirdTuple,
etc
};
正如其他人所提到的,如果您要引用 "like" 数据,则应该创建一个 class。
您可以使用 Tuple
class (See Tuple class on msdn)
Tuple.Create("time", "x", "y"); //creates a Tuple with 3 elements, aka triple
我建议为此目的定义一个不可变类型并使用字典来存储值,因为对象可以在特定时间位于一个地方。
public class Position
{
public Position(double x, double y)
{
X = x;
Y = y;
}
public double X { get; }
public double Y { get; }
}
像这样使用它:
var data = new Dictionary<TimeSpan, Position>();
//add data in two different ways
data.Add(TimeSpan.FromMilliseconds(10), new Position(0.1, 1));
data[TimeSpan.FromMilliseconds(15)] = new Position(10, 15);
//accessing data
var pos = data[TimeSpan.FromMilliseconds(10)];
//check if data exist for specific time
if (data.ContainsKey(TimeSpan.FromMilliseconds(15)))
{
//do what you want
}
您似乎想在给定时间访问 x/y 职位。您可以考虑将其他答案与字典结合使用。
定义一个包含 x 和 y 位置的简单结构。重要的是这个对象是不可变的。
public struct Position
{
public int X { get; }
public int Y { get; }
public Position(int x, int y)
{
this.X = x;
this.Y = y;
}
}
现在您可以将这些存储在字典中(可能)DateTime
作为键。
Dictionary<DateTime, Position> positions = new Dictionary<DateTime, Position>();
positions.Add(new DateTime(2017,3,29,10,0,0), new Position(10,10));
你可以像
一样阅读你的位置
var pos = positions[new DateTime(2017,3,29,10,0,0)];
Console.WriteLine("{0}:{1}",pos.X,pos.Y);
一个巧妙的小补充是,例如,如果您正在获取这些对象的列表,您希望按时间查找,您可以轻松地将它们变成字典。假设您得到了这些列表(来自其他答案):
public class PositionEntity
{
public DateTime Time {get;set;}
public int X {get;set;}
public int Y {get;set;}
}
作为列表entities
你可以这样做:
IEnumerable<PositionEntity> entities = .... loaded from somewhere
var dict = entities.ToDictionary(k => k.Time, v => new Position(v.X,v.Y));
假设我有 100 组 3 个值。第一个值是时间,第二个值是 x 位置,第三个值是 y 位置。例如,这将是一个点在 100 秒内的 xy 位置。 我有 PHP 背景。在 PHP 中,我会使用这样的 3D 关联数组来完成它:
position["time"]["x"]["y"]
所以我可以在特定时间访问 x,y 值。你将如何在 C# 中执行此操作?我认为像 list 和 dictionary 这样的泛型可以做到这一点。但我不知道如何为 3D 数据集实现键值。
在 C# 中有类型概念,您需要创建一个具体的自定义类型 struct(值类型)或 class(引用类型)
你可以创建一个新类型,在这种情况下我正在创建 class 就像:
public class Position
{
public DateTime Time {get;set;}
public int X {get;set;}
public int Y {get;set;}
}
现在在某个时候你会有一个 class 位置的实例:
Position postion = new Position(); // object created
positon.Time = DateTime.Now;
position.X = 1;
position.Y =0;
以及从中获取值的相同方式:
DateTime time = position.Time;
int positionX = position.X;
int positionY = position.Y;
我建议阅读 from MSDN here
类型的基础知识对于集合部分,我们有 Framework 提供的 C# 集合,其中包括 Array,List,可用于保存 Position
的多个对象。
希望对您有所帮助!
一个简单的解决方案可能是使用元组,如果这适合您的需要,例如
Example:
var tuple = Tuple.Create(someBytes, moreBytes, myString);
您可以使用 tuple.Item1、tuple.Item2 和 tuple.Item3 访问这些值。
如果需要,当然也可以使用多个元组,例如
例子
var list = new List<Tuple<byte, byte, string>> {
tuple,
anotherTuple,
thirdTuple,
etc
};
正如其他人所提到的,如果您要引用 "like" 数据,则应该创建一个 class。
您可以使用 Tuple
class (See Tuple class on msdn)
Tuple.Create("time", "x", "y"); //creates a Tuple with 3 elements, aka triple
我建议为此目的定义一个不可变类型并使用字典来存储值,因为对象可以在特定时间位于一个地方。
public class Position
{
public Position(double x, double y)
{
X = x;
Y = y;
}
public double X { get; }
public double Y { get; }
}
像这样使用它:
var data = new Dictionary<TimeSpan, Position>();
//add data in two different ways
data.Add(TimeSpan.FromMilliseconds(10), new Position(0.1, 1));
data[TimeSpan.FromMilliseconds(15)] = new Position(10, 15);
//accessing data
var pos = data[TimeSpan.FromMilliseconds(10)];
//check if data exist for specific time
if (data.ContainsKey(TimeSpan.FromMilliseconds(15)))
{
//do what you want
}
您似乎想在给定时间访问 x/y 职位。您可以考虑将其他答案与字典结合使用。
定义一个包含 x 和 y 位置的简单结构。重要的是这个对象是不可变的。
public struct Position
{
public int X { get; }
public int Y { get; }
public Position(int x, int y)
{
this.X = x;
this.Y = y;
}
}
现在您可以将这些存储在字典中(可能)DateTime
作为键。
Dictionary<DateTime, Position> positions = new Dictionary<DateTime, Position>();
positions.Add(new DateTime(2017,3,29,10,0,0), new Position(10,10));
你可以像
一样阅读你的位置var pos = positions[new DateTime(2017,3,29,10,0,0)];
Console.WriteLine("{0}:{1}",pos.X,pos.Y);
一个巧妙的小补充是,例如,如果您正在获取这些对象的列表,您希望按时间查找,您可以轻松地将它们变成字典。假设您得到了这些列表(来自其他答案):
public class PositionEntity
{
public DateTime Time {get;set;}
public int X {get;set;}
public int Y {get;set;}
}
作为列表entities
你可以这样做:
IEnumerable<PositionEntity> entities = .... loaded from somewhere
var dict = entities.ToDictionary(k => k.Time, v => new Position(v.X,v.Y));