Unity-C#列表List解析
👉前言
List简介:
在 Unity 中,List<T >(T是一种数据类型,比如int、string、GameObject等)是一个非常有用的集合类型,它位于System.Collections.Generic命名空间下。它可以动态地调整大小,能够方便地添加、删除和访问元素。
博客将会介绍Listd的使用方法。希望这篇博客对Unity的开发者有所帮助。
大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。
欢迎点赞评论哦.下面就让我们进入正文吧 !
👉一、常用API
👉1-1、创建List对象
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>();
}
}
👉1-2、添加元素
语法:listName.Add(item);
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<string> stringList = new List<string>();
stringList.Add("Hello");
stringList.Add("World");
}
}
👉1-3、将一个元素插入到指定索引位置
语法:listName.Insert(index, item);
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<float> floatList = new List<float>();
floatList.Add(1.0f);
floatList.Add(3.0f);
floatList.Insert(1, 2.0f);
}
}
打印出来就是 1.0,2.0,3.0
👉1-4、访问List中元素
可以通过索引来访问List中的元素,索引从 0 开始。
语法:T element = listName[index];
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
int secondElement = intList[1];
Debug.Log(secondElement);
}
}
👉1-5、获取元素数量
Count属性:返回List中元素的个数。
语法:int count = listName.Count;
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<GameObject> gameObjectList = new List<GameObject>();
gameObjectList.Add(new GameObject());
gameObjectList.Add(new GameObject());
int count = gameObjectList.Count;
Debug.Log(count);
}
}
👉1-6、删除元素
Remove(T item):从List中删除指定的元素(如果有多个相同元素,只删除第一个)。
语法:listName.Remove(item);
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<string> stringList = new List<string>(){"apple", "banana", "apple"};
stringList.Remove("apple");
}
}
👉1-6、删除指定索引位置的元素
RemoveAt(int index):从List中删除指定索引位置的元素。
语法:listName.RemoveAt(index);
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
intList.RemoveAt(1);
}
}
👉1-7、清空List元素
Clear()方法:移除List中的所有元素。
语法:listName.Clear();
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<Transform> transformList = new List<Transform>();
transformList.Add(transform);
transformList.Clear();
}
}
👉1-8、查找元素
IndexOf(T item):返回指定元素在List中的第一个索引,如果不存在则返回 - 1。
语法:int index = listName.IndexOf(item);
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<string> stringList = new List<string>(){"apple", "banana", "apple"};
int index = stringList.IndexOf("apple");
int index1 = stringList.IndexOf("app");
Debug.Log (index+" "+index1);
}
}
👉1-9、检查list是否包含元素
1.Contains(T item):检查List是否包含指定元素,返回true或false。
语法:bool contains = listName.Contains(item);
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<float> floatList = new List<float>() {1.0f, 2.0f, 3.0f};
bool containsTwo = floatList.Contains(2.0f);
}
}
👉1-10、排序List中的元素
Sort()方法:对List中的元素进行排序。对于简单数据类型(如int、float、string等),会按照默认的排序规则进行排序。
语法:listName.Sort();
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {3, 1, 2};
intList.Sort();
}
}
打印出来是 1,2,3
👉1-11、反转List中的元素(倒序)
示例:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<int> intList = new List<int>() {1, 2, 3};
//反转列表
intList.Reverse();
}
}
打印出来是3,2,1
👉1-12、遍历List (for)
代码如下:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<string> stringList = new List<string>(){"apple", "banana", "cherry"};
for (int i = 0; i < stringList.Count; i++)
{
Debug.Log(stringList[i]);
}
}
}
👉1-13、遍历List (foreach)
代码如下:
using System.Collections.Generic;
public class Example : MonoBehaviour
{
void Start()
{
List<GameObject> gameObjectList = new List<GameObject>();
gameObjectList.Add(new GameObject("Object1"));
gameObjectList.Add(new GameObject("Object2"));
foreach (GameObject obj in gameObjectList)
{
Debug.Log(obj.name);
}
}
}
本次总结的就是List的api详解, 有需要会继续增加功能
如能帮助到你,就帮忙点个赞吧,三连更好哦,谢谢
你的点赞就是对博主的支持,有问题记得留言评论哦!
不定时更新Unity开发技巧,觉得有用记得一键三连哦。么么哒!
- 点赞
- 收藏
- 关注作者
评论(0)