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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15