美文网首页
ToLua的Example示例学习笔记22_UseList

ToLua的Example示例学习笔记22_UseList

作者: 凌枫望星月 | 来源:发表于2020-05-14 13:58 被阅读0次

    这个例子演示了在lua中如何使用String。

                function Exist2(v)
                    return v == 2
                end
    
                function IsEven(v)
                    return v % 2 == 0
                end
    
                function NotExist(v)
                    return false
                end
    
                function Compare(a, b)
                    if a > b then 
                        return 1
                    elseif a == b then
                        return 0
                    else
                        return -1
                    end
                end
    
                function Test(list, list1)        
                    list:Add(123)
                    print('Add result: list[0] is '..list[0])
                    list:AddRange(list1)
                    print(string.format('AddRange result: list[1] is %d, list[2] is %d', list[1], list[2]))
    
                    local const = list:AsReadOnly()
                    print('AsReadOnly:'..const[0])    
    
                    index = const:IndexOf(123)
                    
                    if index == 0 then
                        print('const IndexOf is ok')
                    end
    
                    local pos = list:BinarySearch(1)
                    print('BinarySearch 1 result is: '..pos)
    
                    if list:Contains(123) then
                        print('list Contain 123')
                    else
                        error('list Contains result fail')
                    end
    
                    if list:Exists(Exist2) then
                        print('list exists 2')
                    else
                        error('list exists result fail')
                    end                    
                    
                    if list:Find(Exist2) then
                        print('list Find is ok')
                    else
                        print('list Find error')
                    end
    
                    local fa = list:FindAll(IsEven)
    
                    if fa.Count == 2 then
                        print('FindAll is ok')
                    end
    
                    --注意推导后的委托声明必须注册, 这里是System.Predicate<int>
                    local index = list:FindIndex(System.Predicate_int(Exist2))
    
                    if index == 2 then
                        print('FindIndex is ok')
                    end
    
                    index = list:FindLastIndex(System.Predicate_int(Exist2))
    
                    if index == 2 then
                        print('FindLastIndex is ok')
                    end                
                    
                    index = list:IndexOf(123)
                    
                    if index == 0 then
                        print('IndexOf is ok')
                    end
    
                    index = list:LastIndexOf(123)
                    
                    if index == 0 then
                        print('LastIndexOf is ok')
                    end
    
                    list:Remove(123)
    
                    if list[0] ~= 123 then
                        print('Remove is ok')
                    end
    
                    list:Insert(0, 123)
    
                    if list[0] == 123 then
                        print('Insert is ok')
                    end
    
                    list:RemoveAt(0)
    
                    if list[0] ~= 123 then
                        print('RemoveAt is ok')
                    end
    
                    list:Insert(0, 123)
                    list:ForEach(function(v) print('foreach: '..v) end)
                    local count = list.Count      
    
                    list:Sort(System.Comparison_int(Compare))
                    print('--------------sort list over----------------------')
                                    
                    for i = 0, count - 1 do
                        print('for:'..list[i])
                    end
    
                    list:Clear()
                    print('list Clear not count is '..list.Count)
                end
    
        protected override void OnLoadFinished()
        {
        
            base.OnLoadFinished();
            luaState.DoString(script, "UseList.cs");
            List<int> list1 = new List<int>();
            list1.Add(1);
            list1.Add(2);
            list1.Add(4);
    
            LuaFunction func = luaState.GetFunction("Test");
            func.BeginPCall();
            func.Push(new List<int>());
            func.Push(list1);
            func.PCall();
            func.EndPCall();
            func.Dispose();
            func = null;        
        }
    

    一些List的用法,实际用到回过来看吧。

    相关文章

      网友评论

          本文标题:ToLua的Example示例学习笔记22_UseList

          本文链接:https://www.haomeiwen.com/subject/gkdtohtx.html