怎么样得到一个字符串在一个二维字符串数组中的位置

.Net技术 码拜 8年前 (2016-05-25) 1685次浏览
string[,] strSet = new string[,]
        {
            { "aa", "bb", "cc" },
            { "dd", "ee", "ff"}, 
            { "gg", "hh", "ii" } 
        };

这是一个二维字符串数组,本人想知道“ee”在这个数组中的位置,该怎么做。
对于一维数组,可以用

Array.IndexOf(str, s)

确定字符串s在字符串数组str中的位置,二维数组有没有相似的做法?
谢谢大家

解决方案

10

class inPoint
{
    public int x { set; get; }
    public int y { set; get; }
    public List<inPoint> ResList = new List<inPoint>();
    public List<inPoint> getPoint(string[,] p_Arr,string Key)
    {              
        for(int row=0;row<=p_Arr.GetUpperBound(0);row++)
        {
            for(int col=0;col<=p_Arr.GetUpperBound(1);col++)
            {
                if(p_Arr[row,col]==Key)
                {
                    ResList.Add(new inPoint { x = row, y = col });
                }
            }
        }
        return ResList;
    }
}
//调用
string[,] strSet = new string[,]
{
    { "aa", "bb", "cc" },
    { "dd", "ee", "ff"}, 
    { "gg", "hh", "ii" },
    { "ee", "pp", "qq"}
};
inPoint p=new inPoint();
List<inPoint> list = p.getPoint(strSet, "ee");
foreach (inPoint p1 in list)
{
    Response.Write(p1.x.ToString() + "--" + p1.y.ToString() + "<br />");
}

输出:
1–1
3–0
可以找到全部满足条件的位置坐标,在你原来基础上增加了一组数据

15

            string[,] strSet = new string[,]
            {
                { "aa", "bb", "cc" },
                { "dd", "ee", "ff" }, 
                { "gg", "hh", "ii" },
            };
            var r = strSet.GetLength(0);
            var c = strSet.GetLength(1);
            int i;
            for (i = 0; i < strSet.Length; i++) if(strSet[i / r, i % c] == "ee") break;
            
            Console.WriteLine("[{0},{1}]", i / r, i % c);

10

二维数组没有一步到位的方法
1、本人写二维的查询方法,上面已经给出了。
2、把二维数组元素copy到集合,集合自带获取index方法。也可以用你的Array.IndexOf(str, s),先建一个有相同元素的一维数组,获取一维位置index,他在2维数组的位置position = (index/3 ,index%3 )。

5

混分魔王降临~

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么样得到一个字符串在一个二维字符串数组中的位置
喜欢 (0)
[1034331897@qq.com]
分享 (0)