数组和集合实例

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] arr1 = new int[2] {1,2};
int[,] arr2 = new int[2, 3] {
{0,1,2 },
{2,3,4 }
};

Console.WriteLine(arr2[1,1]);

ArrayList
ArrayList alt = new ArrayList();
alt.Add("123");
alt.Add(123);
alt.Add(true);
bool iscontain = alt.Contains("1");
alt.Clear();
/*alt.Insert(0, "abc")*/
;
Console.WriteLine(iscontain);

for(int i = 0; i < alt.Count; i++)
{
Console.WriteLine(alt[i].ToString() + " " + alt[i].GetType().ToString());
}

foreach (var x in alt)
{
Console.WriteLine(x.ToString() + " " + x.GetType().ToString());
}

泛型集合 List
List<string> str_list = new List<string>();
str_list.Add("a");
str_list.Add("b");
str_list.Add("c");
str_list.Add("d");

foreach(string x in str_list)
{
Console.WriteLine(x);
}


哈希表hashtable

Hashtable ht = new Hashtable();
ht.Add("1","a");
ht.Add(2, "b");
ht.Add(3, false);
ht.Add("x", 3.14);
Console.WriteLine(ht[2]);
foreach(var x in ht.Keys)
{
Console.WriteLine(ht[x]);
}

字典表 Dictionary

Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("a", 3);
dic.Add("b", 4);
dic.Add("c", 5);
dic.Add("d", 6);
dic.Add("e", 7);

foreach(var x in dic.Keys)
{
Console.WriteLine(x);
}

队列 queue

Queue que = new Queue();
que.Enqueue("张三");
que.Enqueue("李四");
que.Enqueue("王五");
que.Enqueue("赵六");
Console.WriteLine("现在的长度是" + que.Count);
Console.WriteLine(que.Dequeue());
Console.WriteLine("现在的长度是" + que.Count);

堆栈 stack

Stack st = new Stack();
st.Push("a");
st.Push("b");
st.Push("c");
st.Push("d");

Console.WriteLine(st.Count);
Console.WriteLine(st.Pop());
Console.WriteLine(st.Count);

Console.ReadLine();


int[] arr1 = new int[2] { 1, 2 };
int[,] arr2 = new int[2, 3]{
{0,1,2},{2,3,4}
};
Console.WriteLine(arr2[1, 1]);

ArrayList 特点不需要不需要限长度,不需要规定类型,缺点:键值只能是0,1,2,3往后排:

ArrayList alt = new ArrayList();
alt.Add("123");
alt.Add(123);
alt.Add(true);
alt.Remove(123); 从其中移除 clear 是全部清空的意思
bool iscontain=alt.Contains("123"); 看看里面是否包含"123",如果有就会显示"true",如果没有就会显示false;
alt.Clear(); 全部清除:
alt.Insert(0,"abc"); 意思是往 0 位置插入一个字符串"123";

Console.WriteLine(alt);
for (int i = 0; i < alt.Count;i++ ) {
Console.WriteLine(alt[i].ToString()+" "+alt[i].GetType().ToString());

}


var类型是 万能类型
foreach(var x in alt){

Console.WriteLine(x.ToString()+" "+alt[i].GetType().ToString)



}
泛型集合 list 需要规定类型,不需要规定长度 也不行规定键值 也是从0,1,2,3开始排

List<string> str_list = new List<string>();
str_list.Add("a");
str_list.Add("b");
str_list.Add("c");
str_list.Add("d");

foreach (string x in str_list) {

Console.WriteLine(x);


}


哈希表hashtable 不错在顺序的概念


Hashtable ht = new Hashtable();
ht.Add("1", "a");
ht.Add(2, "b");
ht.Add(3, false);
ht.Add("x", 3.14);
//Console.WriteLine(ht[2]);
foreach (var x in ht.Values) {

Console.WriteLine(x);


}
foreach (var x in ht.Keys)
{

Console.WriteLine(x); //取全部的值
Console.WriteLine(ht[x]);

}

 

字典表 Dictionary
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("a", 3);
dic.Add("b", 4); 前面的是key键 后面的是value值
dic.Add("c", 5);
dic.Add("d", 6);
dic.Add("e", 7);
dic.Add("f", 8);


foreach (var x in dic.Keys) {

Console.WriteLine(x);
}


队列 queue 先进先出
Queue que = new Queue();
que.Enqueue("张三");
que.Enqueue("李四");
que.Enqueue("王五");
que.Enqueue("赵六");
Console.WriteLine("现在的长度是" + que.Count);
Console.WriteLine(que.Dequeue()); //dequeue()函数用于移除每个匹配元素的指定队列中的第一个函数,并执行被移除的函数。
Console.WriteLine("现在的长度" + que.Count);

堆栈 stack 先进后出


Stack st = new Stack();
st.Push("a");
st.Push("b");
st.Push("c");
st.Push("d");
st.Push("e");

Console.WriteLine(st.Count);
Console.WriteLine(st.Pop());
Console.WriteLine(st.Count);

 

Console.ReadLine();

}
}
}

转载于:https://www.cnblogs.com/liuyubin0629/p/6993342.html

内容概要:本文深入探讨了Kotlin语言在函数式编程跨平台开发方面的特性优势,结合详细的代码案例,展示了Kotlin的核心技巧应用场景。文章首先介绍了高阶函数Lambda表达式的使用,解释了它们如何简化集合操作回调函数处理。接着,详细讲解了Kotlin Multiplatform(KMP)的实现方式,包括共享模块的创建平台特定模块的配置,展示了如何通过共享业务逻辑代码提高开发效率。最后,文章总结了Kotlin在Android开发、跨平台移动开发、后端开发Web开发中的应用场景,并展望了其未来发展趋势,指出Kotlin将继续在函数式编程跨平台开发领域不断完善发展。; 适合人群:对函数式编程跨平台开发感兴趣的开发者,尤其是有一定编程基础的Kotlin初学者中级开发者。; 使用场景及目标:①理解Kotlin中高阶函数Lambda表达式的使用方法及其在实际开发中的应用场景;②掌握Kotlin Multiplatform的实现方式,能够在多个平台上共享业务逻辑代码,提高开发效率;③了解Kotlin在不同开发领域的应用场景,为选择合适的技术栈提供参考。; 其他说明:本文不仅提供了理论知识,还结合了大量代码案例,帮助读者更好地理解实践Kotlin的函数式编程特性跨平台开发能力。建议读者在学习过程中动手实践代码案例,以加深理解掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值