纳金网

标题: C#经常遇到得数组随机排序 [打印本页]

作者: 王者再临    时间: 2014-12-31 21:10
标题: C#经常遇到得数组随机排序
  1. using unityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class list : MonoBehaviour {
  5.     List<string> ListName = new List<string>();

  6.     void Start () {
  7.         ListName.Add ("a1");
  8.         ListName.Add ("b2");
  9.         ListName.Add ("c3");
  10.         ListName.Add ("d4");
  11.         ListName.Add ("e5");
  12.         ListName.Add ("f6");

  13.         ListName = RandName (ListName);
  14.         foreach (string name in ListName) {
  15.             print(name);        
  16.         }

  17.     }
  18. //suiji shuzu
  19.     List<string>RandName(List<string>OldName)
  20.     {
  21.         List<string> NListName=new List<string>();
  22.         while (NListName.Count<OldName.Count) {
  23.             int i=Random.Range(0,OldName.Count);
  24.             if(!NListName.Contains(OldName[i]))
  25.                 NListName.Add(OldName[i]);
  26.         }
  27.         return NListName;
  28.     }


  29. }
复制代码

作者: 我不再年轻    时间: 2015-1-3 11:11
不错, 学习了!




欢迎光临 纳金网 (http://www.narkii.com/club/) Powered by Discuz! X2.5