java 元组Tuple的使用方法
有三种使用方法,直接上代码
1、类似于数据库表的一行,对比于数组集合,允许元素类型不同,允许,
2、Tuple比较类似于数组等集合类型,不同的是,Tuple允许每个元素的类型不同,并且是通过泛型参数来指定元素类型,而且必须严格按照顺序来赋值。
3、Tuple不仅仅是强类型的集合,而且是有次序的,可谓是“等级森严”。
//方法1 实例方法创建元组
Tuple<int, string> t = new Tuple<int, string>(300,"xxx");
//方法2 静态方法创建元组
Tuple<string, int, string> t2 = Tuple.Create<string, int, string>("小明", 33, "天边有朵云");
//多线程编程中还可以用Tuple来充当要传递的参数
Tuple<string, string> t3 = new Tuple<string, string>("desk","table");
string demo = "";
Task newtask = new Task(p =>
{
Tuple<string, string> ttmp = p as Tuple<string, string>;
Console.WriteLine(ttmp.Item1 + ttmp.Item2);
<span class="token punctuation">}</span><span class="token punctuation">,</span> t3<span class="token punctuation">)</span><span class="token punctuation">;</span>
newtask<span class="token punctuation">.</span><span class="token function">Start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Thread<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span><span class="token number">100000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> t3<span class="token punctuation">)</span><span class="token punctuation">;</span>
newtask<span class="token punctuation">.</span><span class="token function">Start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Thread<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span><span class="token number">100000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)