C# 查找一组数(子数组)每个元素在(另一组数)父数组的索引

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;
        }
    }

}

res

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈言必行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值