Dictionary&List&Array
helloz 4/3/2024 Unity3d
reference:
https://blog.csdn.net/weixin_43796392/article/details/121135334 (opens new window)
functions
functions | parameter | describe |
---|---|---|
newDictionary(Type,Type) | 1.key Type 2.value Type return Dictionary | local dic2 = newDictionary(System.String,System.Object) This is the key as a string and the data as an object |
newList(Type) | 1.Type return List | local ls2 = newList(System.Int32) list of int type |
newArray(Type,Int) | 1.Type 2.Length return Array | If Length is less than or equal to 0, the return result is nil local arr = newArray(System.Int32,5) |
c# Dictionary
------example 1--不推荐-------
--先声明字典类型--
local sendDic=System.Collections.Generic.Dictionary(System.String,System.Object)
--实例数据结构--
local backDic=sendDic()
--添加数据
backDic:Add('cmd',"join.twoCompleted")
backDic:Add('result',true)
------example 2---推荐-----
local dic2 = newDictionary(System.String,System.Object)
dic2:Add('1','123')
dic2:Add('2',100)
--遍历方法--
for k,v in pairs(dic2) do
print(k,v)
end
print(dic2:get_Item('1')) -- 123
dic2:set_Item('1',666)
print(dic2['1']) --null
print(dic3:TryGetValue('1')) --return bool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
C# List
------example 1--不推荐-------
--先声明list类型--
local list_int= System.Collections.Generic.List(System.Int32)
--实例数据结构--
local ls = list_int()
--添加数据--
ls:Add(2)
ls:Add(8)
------example 2---推荐-----
local ls2 = newList(System.Int32)
ls2:Add(2)
ls2:Add(8)
ls2:Remove(8) --item
ls2:RemoveAt(0) -- index
--遍历--
for i=0,ls2.Count-1 do
log(ls2[i])
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
C# Array
--int
local arr = newArray(System.Int32,5)
arr[3]=90
print(arr.Length)
for i=0,arr.Length-1 do
print(arr[i])
end
--float
local arr = newArray(System.Single,3)
arr[2]=90.5
print(arr.Length)
for i=0,arr.Length-1 do
print(arr[i])
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15