C# 查找一个指定的值是否在一个数组(或者是list)中 ,,,
/// <summary>
/// 查找 (数值在列表中的位置)
/// 不存在返回-1
/// </summary>
/// <param name="numvalue">值</param>
/// <param name="handcard">列表</param>
/// <returns></returns>
public static int Seach(int numvalue, List<int> handcard)
{
int n = -1;
for (int i = 0; i < handcard.Count; i++)
{
if (numvalue== handcard[i])
{
n = i;
return n;
}
}
return n;
}
在查找一个子数组,在其父数组中的位置的时候遇到一个问题,,,
若没有重复的值还好,直接按照下面的代码写就可以了,,返回的list 是每个子数组中的每个元素在父数组的中位置,,,若不存在则用-1占位置,,,
/// <summary>
/// 查找 ()
/// (找一组牌在手牌的位置id)
/// 不存在返回-1
/// </summary>
/// <param name="value1">牌值数组</param>
/// <param name="handcard">父list</param>
/// <returns></returns>
public static List<int> Seach(List<int> value1, List<int> handcard)
{
List<int> templist = new List<int>();
for (int i = 0; i < value.Count; i++)
{
for (int j = 0; j < handcard.Count; j++)
{
if (value1[i] == handcard[j])
{
templist.Add(j);
}
}
templist.Add(-1);
}
return templist;
}
若使用上面方法进行查找一组数在另一组数中的索引id,,,当要查找的数中有相同的数时,那么则会出现,,每次查找都返回的都是第一次出现的索引,,,这时就需要做些限制 (需要注意的是传递的此方法使用的是引用类型的参数,不能在方法内修改传递进来的list中的值,若修改有影响则需要新建list来做为参数传递,进行查找)、、、
看下面的例子:
using System;
using System.Collections.Generic;
namespace unitygametest
{
class Program
{
static void Main(string[] args)
{
//要查找的list
List<int> findCards = new List<int> { 34, 2, 2 };
//被查找的的list (不想被改变的list)
List<int> Cards = new List<int> { 31, 2, 2, 17, 12, 5, 41, 18, 33, 35, 43, 37, 7, 8, 25, 29, 54, 3, 12, 46, 24, 34, 31, 51, 9, 1, 28, 4 };
List<int> newCards = new List<int>() ; //通过新建来解决问题...
newCards.AddRange(Cards);
List<int> res1ist = new List<int>();
Console.WriteLine("要查找的 findCards ...");
for (int i = 0; i < findCards.Count; i++)
{
Console.Write(findCards[i] + ",");
}
Console.WriteLine();
Console.WriteLine("被查找的 Cards ...");
for (int i = 0; i < Cards.Count; i++)
{
Console.Write(Cards[i] + ",");
}
Console.WriteLine();
res1ist = Seach(findCards, newCards);
Console.WriteLine("查找的结果 res1ist ...");
for (int i = 0; i < res1ist.Count; i++)
{
Console.Write(res1ist[i] + ",");
}
Console.WriteLine();
Console.WriteLine("被新建的 newCards ... ");
for (int i = 0; i < newCards.Count; i++)
{
Console.Write(newCards[i] + ",");
}
Console.ReadLine();
}
/// <summary>
/// list作为参数形式查找
/// 查找一个子list中所有元素在一个父list中的索引
/// </summary>
/// <param name="value"></param>
/// <param name="handcard"></param>
/// <returns></returns>
static List<int> Seach(List<int> findcard, List<int> handcard)
{
List<int> list_id = new List<int>();
for (int i = 0; i < findcard.Count; i++)
{
for (int j = 0; j < handcard.Count; j++)
{
if (findcard[i] == handcard[j])
{
list_id.Add(j);
//value[i] = -1;
handcard[j] = -2; //这样会改变原来list 的值
break;
}
}
}
return list_id;
}
/// <summary>
/// 数组的重载
/// 数组也是引用类型,也存在上面list的那个问题
/// </summary>
/// <param name="value"></param>
/// <param name="handcard"></param>
/// <returns></returns>
static List<int> Seach(int[] findcard, int[] handcard)
{
List<int> list_id = new List<int>();
for (int i = 0; i < findcard.Length; i++)
{
for (int j = 0; j < handcard.Length; j++)
{
if (findcard[i] == handcard[j])
{
list_id.Add(j);
//findcard[i] = -1;
handcard[j] = -2;
break;
}
}
}
return list_id;
}
}
}