108 lines
3.6 KiB
C#
108 lines
3.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
|
||
namespace Lesson07
|
||
{
|
||
class Program
|
||
{
|
||
static void Main(string[] args)
|
||
{
|
||
// ========== List<T> ==========
|
||
Console.WriteLine("=== List<T> 动态数组 ===\n");
|
||
|
||
List<string> transcriptHistory = new List<string>();
|
||
|
||
// 添加识别结果
|
||
transcriptHistory.Add("今天天气真不错");
|
||
transcriptHistory.Add("大家一起加油");
|
||
transcriptHistory.Add("这个功能很简单");
|
||
|
||
Console.WriteLine($"共有 {transcriptHistory.Count} 条记录:");
|
||
foreach (string text in transcriptHistory)
|
||
{
|
||
Console.WriteLine($" - {text}");
|
||
}
|
||
|
||
// 插入
|
||
transcriptHistory.Insert(1, "新插入的文字");
|
||
Console.WriteLine($"\n插入后共有 {transcriptHistory.Count} 条记录:");
|
||
foreach (string text in transcriptHistory)
|
||
{
|
||
Console.WriteLine($" - {text}");
|
||
}
|
||
|
||
// 删除
|
||
transcriptHistory.Remove("大家一起加油");
|
||
Console.WriteLine($"\n删除后共有 {transcriptHistory.Count} 条记录");
|
||
|
||
// 初始化器
|
||
List<string> fruits = new List<string>() { "苹果", "香蕉", "橙子" };
|
||
Console.WriteLine($"\n水果列表:{string.Join(", ", fruits)}");
|
||
|
||
// ========== Dictionary<K, V> ==========
|
||
Console.WriteLine("\n=== Dictionary<K, V> 键值对 ===\n");
|
||
|
||
// 设备配置
|
||
Dictionary<string, string> deviceConfig = new Dictionary<string, string>()
|
||
{
|
||
["name"] = "USB麦克风",
|
||
["sampleRate"] = "16000",
|
||
["channels"] = "1"
|
||
};
|
||
|
||
Console.WriteLine("设备配置:");
|
||
foreach (KeyValuePair<string, string> kv in deviceConfig)
|
||
{
|
||
Console.WriteLine($" {kv.Key}: {kv.Value}");
|
||
}
|
||
|
||
// 访问单个值
|
||
Console.WriteLine($"\n设备名称: {deviceConfig["name"]}");
|
||
|
||
// 安全访问
|
||
if (deviceConfig.TryGetValue("notExist", out string value))
|
||
{
|
||
Console.WriteLine($"找到: {value}");
|
||
}
|
||
else
|
||
{
|
||
Console.WriteLine("键不存在");
|
||
}
|
||
|
||
// 设置选项
|
||
Dictionary<string, bool> settings = new Dictionary<string, bool>()
|
||
{
|
||
["autoConnect"] = true,
|
||
["vad"] = true,
|
||
["debug"] = false
|
||
};
|
||
|
||
Console.WriteLine("\n设置选项:");
|
||
foreach (string key in settings.Keys)
|
||
{
|
||
string status = settings[key] ? "开启" : "关闭";
|
||
Console.WriteLine($" {key}: {status}");
|
||
}
|
||
|
||
// ========== 数字集合 ==========
|
||
Console.WriteLine("\n=== 数字 List 操作 ===\n");
|
||
|
||
List<int> scores = new List<int>() { 85, 92, 78, 95, 88 };
|
||
|
||
Console.WriteLine($"原始成绩:{string.Join(", ", scores)}");
|
||
|
||
scores.Sort();
|
||
Console.WriteLine($"排序后:{string.Join(", ", scores)}");
|
||
|
||
scores.Reverse();
|
||
Console.WriteLine($"反转后:{string.Join(", ", scores)}");
|
||
|
||
Console.WriteLine($"最高分:{scores[0]}");
|
||
Console.WriteLine($"包含 100? {scores.Contains(100)}");
|
||
Console.WriteLine($"包含 95? {scores.Contains(95)}");
|
||
|
||
Console.ReadLine();
|
||
}
|
||
}
|
||
}
|