golangSlice
lencap

1.C#的泛型集合List

C#
//实例化 初始化
List<string> ls = new List<string> { "北京", "上海", "广州", "深圳" };

//输出容量与长度
Console.WriteLine($"the capacity of citylist is {ls.Capacity},and the length of citylist is {ls.Count}");

//新增元素
ls.Add("成都");

//再次输出容量与长度
Console.WriteLine($"the capacity of citylist is {ls.Capacity},and the length of citylist is {ls.Count}");

//删除元素
ls.Remove("成都");

//再次输出容量与长度
Console.WriteLine($"the capacity of citylist is {ls.Capacity},and the length of citylist is {ls.Count}");

//遍历元素
foreach (string city in ls)
{

}
the capacity of citylist is 4,and the length of citylist is 4
the capacity of citylist is 8,and the length of citylist is 5
the capacity of citylist is 8,and the length of citylist is 4

另外在C#中还提供了很多很多扩展方法来操作泛型集合,这里提一些常用的。

//添加多个元素
public void AddRange(IEnumerable<T> collection);

//删除所有
public void Clear();

//按条件删除
public int RemoveAll(Predicate<T> match);

//按索引进行范围删除
public void RemoveRange(int index, int count);

//遍历操作
public void ForEach(Action<T> action);

//判断是否存在某元素
 public bool Contains(T item);

//按条件判断是否存在
public bool Exists(Predicate<T> match);

//按条件查找指定元素
public List<T> FindAll(Predicate<T> match);

//翻转集合
public void Reverse();

//转换数组
public T[] ToArray();

2.Golang中的切片

C#

1. 初始化-新增-复制

1.1 定义不初始化

//定义不初始化-这个定义不初始化的称为-零值切片
var citySlice0 []string

1.2 定义且初始化

//定义且初始化
var citySlice1 = []string{}
var citySlice2 = []string{"北京", "上海", "广州", "深圳"}

1.3 make定义并初始化

//make定义并初始化
citySlice := make([]string, 4, 10)
fmt.Printf("the citySlice is %vn", citySlice)

1.4 容量与长度

cap()len()
//输出容量和长度
fmt.Printf("the capacity of citySlice is %v,and the length of citySlice is %v n", cap(citySlice), len(citySlice))

1.5 新增

append()
//新增元素
citySlice = append(citySlice, "北京", "上海", "广州", "深圳")
fmt.Printf("the citySlice is %vn", citySlice)
fmt.Printf("the capacity of citySlice is %v,and the length of citySlice is %v n", cap(citySlice), len(citySlice))

//var声明的零值切片最简单的方式便是通过append函数直接使用,无需初始化
var intSliceA []int
intSliceA = append(intSliceA, 1, 2, 3)
fmt.Printf("the intSliceA is %v n", intSliceA)//[1 2 3]

//切片是引用类型 简单的赋值就出现如下结果
intSliceB := intSliceA
intSliceB[0] = 0
fmt.Printf("the intSliceA is %v n", intSliceA) //[0,2,3]

1.6 复制

copy()
//为了不影响赋值操作,只要复制切片才能达到预期的效果, 但是把一个切片复制给另一个切片,目的切片需要分配空间
intSliceC := make([]int, 4, 5)
copy(intSliceC, intSliceA)
fmt.Printf("the intSliceC is %v n", intSliceC) //[0 2 3 0]  第4个元素0,是因为分配了空间,都是零值
intSliceC[0] = 10
fmt.Printf("the intSliceA is %v n", intSliceA) //[0 2 3]
fmt.Printf("the intSliceC is %v n", intSliceC) //[10 2 3 0]

1.7 汇总

//定义不初始化-这个定义不初始化的称为-零值切片
var citySlice0 []string

//定义且初始化
var citySlice1 = []string{}
var citySlice2 = []string{"北京", "上海", "广州", "深圳"}

//make定义并初始化
citySlice := make([]string, 4, 10)
fmt.Printf("the citySlice is %vn", citySlice)
//输出容量和长度
fmt.Printf("the capacity of citySlice is %v,and the length of citySlice is %v n", cap(citySlice), len(citySlice))

//新增元素
citySlice = append(citySlice, "北京", "上海", "广州", "深圳")
fmt.Printf("the citySlice is %vn", citySlice)
fmt.Printf("the capacity of citySlice is %v,and the length of citySlice is %v n", cap(citySlice), len(citySlice))

//新增元素
citySlice = append(citySlice, "成都", "武汉")
fmt.Printf("the citySlice is %vn", citySlice)
fmt.Printf("the capacity of citySlice is %v,and the length of citySlice is %v n", cap(citySlice), len(citySlice))

//var声明的零值切片最简单的方式便是通过append函数直接使用,无需初始化
var intSliceA []int
intSliceA = append(intSliceA, 1, 2, 3)
fmt.Printf("the intSliceA is %v n", intSliceA)//[1 2 3]

//切片是引用类型 简单的赋值就出现如下结果
intSliceB := intSliceA
intSliceB[0] = 0
fmt.Printf("the intSliceA is %v n", intSliceA) //[0,2,3]

//为了不影响赋值操作,只要复制切片才能达到预期的效果, 但是把一个切片复制给另一个切片,目的切片需要分配空间
intSliceC := make([]int, 4, 5)
copy(intSliceC, intSliceA)
fmt.Printf("the intSliceC is %v n", intSliceC) //[0 2 3 0]  第4个元素0,是因为分配了空间,都是零值
intSliceC[0] = 10
fmt.Printf("the intSliceA is %v n", intSliceA) //[0 2 3]
fmt.Printf("the intSliceC is %v n", intSliceC) //[10 2 3 0]
the citySlice is [   ]
the capacity of citySlice is 10,and the length of citySlice is 4 
the citySlice is [    北京 上海 广州 深圳]
the capacity of citySlice is 10,and the length of citySlice is 8
the citySlice is [    北京 上海 广州 深圳 成都 武汉]
the capacity of citySlice is 10,and the length of citySlice is 10

the intSliceA is [1 2 3]
the intSliceA is [0 2 3]
the intSliceC is [0 2 3 0]
the intSliceA is [0 2 3]
the intSliceC is [10 2 3 0]

2. 切

切片之所以叫切片,着重点在切

“数组上切,就成了切片,在切片上切,就成了切切片(#^.^#),当然不是,还是叫切片。”

//the intSliceC is [10 2 3 0]

//从索引1切到最后
intSliceD := intSliceC[1:]
fmt.Printf("the intSliceD is %v n", intSliceD) // [2 3 0]

//从索引1切到索引2,按照数学知识就是左闭右开[1,3)
intSliceE := intSliceC[1:3]
fmt.Printf("the intSliceE is %v n", intSliceE) //[2 3]

//从索引0切到n-1  0,1,2
intSliceF := intSliceC[:3]
fmt.Printf("the intSliceF is %v n", intSliceF) //[10 2 3] 

//再次验证长度与容量
fmt.Printf("the capacity of intSliceF is %v,and the length of intSliceF is %v n", cap(intSliceF), len(intSliceF))

//
fmt.Printf("the intSliceC is %v n", intSliceC) //[10 2 3 0]
the intSliceD is [2 3 0]
the intSliceE is [2 3]
the intSliceF is [10 2 3] 
the capacity of intSliceF is 5,and the length of intSliceF is 3
the intSliceC is [10 2 3 0]

3. 删除

golangappend()
func append(slice []Type, elems ...Type) []Type
... Typeparams T[] xC#Golang...
intSliceC = append(intSliceC[:1], intSliceC[2:]...) 
fmt.Printf("the intSliceC is %v n", intSliceC) // [10 2 0]
fmt.Printf("the capacity of intSliceC is %v,and the length of intSliceC is %v n", cap(intSliceC), len(intSliceC))
the intSliceC is [10 2 0]
the capacity of intSliceC is 5,and the length of intSliceC is 3

4. 判断切片是否为空

lennil
len(s) == 0

5. 遍历

s := []int{1, 3, 5}

for i := 0; i < len(s); i++ {
    fmt.Println(i, s[i])
}
for index, value := range s {
    fmt.Println(index, value)
}
0 1
1 3
2 5
0 1
1 3
2 5

再次强调:这个系列并不是教程,如果想系统的学习,博主可推荐学习资源。