Dictionary<string, float[]> 示例
时间: 2023-12-24 18:05:12 浏览: 24
Dictionary<string, float[]> 是一个 C# 中的数据结构,它可以用来存储一组以字符串为键、浮点数数组为值的数据。下面是一个示例:
```csharp
Dictionary<string, float[]> myDict = new Dictionary<string, float[]>();
// 添加数据
myDict.Add("key1", new float[] { 1.0f, 2.0f, 3.0f });
myDict.Add("key2", new float[] { 4.0f, 5.0f, 6.0f });
// 访问数据
float[] value1 = myDict["key1"];
float[] value2 = myDict["key2"];
// 遍历数据
foreach (KeyValuePair<string, float[]> item in myDict)
{
string key = item.Key;
float[] value = item.Value;
Console.WriteLine($"{key}: [{string.Join(", ", value)}]");
}
```
在上面的示例中,我们首先创建了一个 `myDict` 的实例,然后使用 `Add` 方法向其中添加了两组数据。接着,我们可以通过索引操作符 `[]` 访问这些数据。最后,我们使用 `foreach` 循环遍历了这个字典中的所有数据,并输出了它们的键和值。
相关问题
Dictionary<string, float[]> 循环float
你可以使用 foreach 循环来遍历 Dictionary<string, float[]> 中的所有 float 数组。
以下是一个示例代码:
```csharp
Dictionary<string, float[]> dict = new Dictionary<string, float[]>();
// 添加一些数据
dict.Add("array1", new float[] { 1.0f, 2.0f, 3.0f });
dict.Add("array2", new float[] { 4.0f, 5.0f, 6.0f });
// 遍历所有 float 数组
foreach (var kvp in dict)
{
string key = kvp.Key;
float[] array = kvp.Value;
// 遍历 float 数组
foreach (float f in array)
{
Console.WriteLine("{0}: {1}", key, f);
}
}
```
在上面的示例中,我们首先创建了一个包含两个 float 数组的 Dictionary。然后,我们使用 foreach 循环遍历 Dictionary 中的每个键值对。在循环中,我们分别获取键和值,并使用另一个 foreach 循环遍历每个 float 数组中的元素,并在控制台上输出键和元素的值。
public interface IState { string Name { get; } Action EnterAction { get; } Action UpdateAction { get; } Action ExitAction { get; } } public class StateMachine<T> where T : IState { private T currentState; private Dictionary<string, IState> stateDict = new Dictionary<string, IState>(); private float updateInterval = 0.1f; private float updateTimer = 0f; public void AddState(T state) { stateDict[state.Name] = state; } public void SetState(string stateName) { if (currentState != null) { currentState.ExitAction(); } currentState = (T)stateDict[stateName]; currentState.EnterAction(); } public void Update() { updateTimer += Time.deltaTime; if (updateTimer >= updateInterval) { if (currentState != null) { currentState.UpdateAction(); } updateTimer = 0f; } } } public class IdleState : IState { public string Name { get { return "Idle"; } } public Action EnterAction { get { return () => { /* Do idle behavior */ }; } } public Action UpdateAction { get { return () => { /* Do idle behavior */ }; } } public Action ExitAction { get { return () => { /* Do idle behavior */ }; } } } public class WalkingState : IState { public string Name { get { return "Walking"; } } public Action EnterAction { get { return () => { /* Do walking behavior */ }; } } public Action UpdateAction { get { return () => { /* Do walking behavior */ }; } } public Action ExitAction { get { return () => { /* Do walking behavior */ }; } } } public class RunningState : IState { public string Name { get { return "Running"; } } public Action EnterAction { get { return () => { /* Do running behavior */ }; } } public Action UpdateAction { get { return () => { /* Do running behavior */ }; } } public Action ExitAction { get { return () => { /* Do running behavior */ }; } } 请使用这段代码写出具体使用示例代码}
// 创建状态机
StateMachine<IState> stateMachine = new StateMachine<IState>();
// 添加状态
stateMachine.AddState(new IdleState());
stateMachine.AddState(new WalkingState());
stateMachine.AddState(new RunningState());
// 设置初始状态
stateMachine.SetState("Idle");
// 循环更新状态
while (true) {
stateMachine.Update();
}