kingBook

导航

C# 去除List中的重复项

using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour{
	void Start(){
		string[] strs=new string[]{"a","b","c","a","e","c","","f",""};
		List<string> list=new List<string>(strs);
		List<string> layerNameList=new List<string>(strs);
		for (int i=0; i<layerNameList.Count;i++){
			for (int j=layerNameList.Count-1;j>i;j--){
				if (layerNameList[i]==layerNameList[j]){
					layerNameList.RemoveAt(j);
				}
			}
		}

		/*for (int i=0; i<layerNameList.Count;i++){
			Debug.Log(layerNameList[i]);
		}*/

	}
}
使用Enumerable.Distinct方法
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Test:MonoBehaviour{
	private void Start(){
		string[] strs=new string[]{"a","b","c","a","e","c","","f",""};
		List<string> list=strs.Distinct().ToList();//using System.Linq;
		
		/*for (int i=0; i<list.Count;i++){
			Debug.Log(list[i]);
		}*/
	}
}

posted on 2019-08-03 01:04  kingBook  阅读(2713)  评论(0编辑  收藏  举报