【问题标题】:How to short text alphabetical whit giving value for each character如何缩短文本的字母顺序,为每个字符赋予价值
【发布时间】:2016-09-10 09:38:39
【问题描述】:

我在想短字符串是否具有字符值的好方法。就像 A = 0,B = 1...

我编写了一些代码,但最好的答案是错误的,我想我知道问题出在哪里,但我不知道问题出在哪里。

代码如下:

void Start()
{
    i = Inventory.instance;
    List<Items> items = i.returnList("Food");
    Debug.Log(items.Count); List<Items> sItems = GetListSetList(items);

    foreach (Items item in sItems)
    {
        Debug.Log(item.name);
    }
}

列表项 = i.returnList ("食物");

public List<Items> returnList(string tag)
{
    List<Items> classifiedItemSet = new List<Items>();
    foreach (Items i in items)
    {
        if (i.tag == tag)
        {
            classifiedItemSet.Add(i);
        }
    }
    return classifiedItemSet;
}

List sItems = GetListSetList (items);

 private List<Items> GetListSetList(List<Items> itemS)
{
    foreach (Items item in itemS)
    {
        GetAlphabetaValue(item);
    }
    List<Items> shorted = new List<Items>();
    Items[] hold = new Items[itemS.Count];
    foreach (Items item in itemS)
    {
        for (int x = 0; x < hold.Length; x++)
        {
            if (hold[x] == null)
            {
                hold[x] = item;
                break;
            }
            else
            {
                bool PassIt = true;
                for (int c_value = 0; c_value < item.Alphabet_value.Length; c_value++)                    
                    if (item.Alphabet_value[c_value] > hold[x].Alphabet_value[c_value])                        
                        PassIt = false;                       

                if (PassIt)
                {
                    for (int h_pos = hold.Length - 1; h_pos > x; h_pos--)                       
                        if (hold[h_pos] != null)
                            hold[h_pos] = hold[h_pos - 1];                       

                    hold[x] = item;
                    break; // If i use this break i get error ("NullReferenceException")
                }
                else continue;                             
            }
        }
    }
    for (int x = 0; x < hold.Length; x++)        
        shorted.Add(hold[x]);        
    return shorted;
}

这个 void 的开始我给每个项目名称字符串中的每个字符一些值。就是这部分:GetAlphabetaValue (item);哦,抱歉将其命名为 AlphaBeta :) 好吧,我是如何得到这个值的:

 private void GetAlphabetaValue(Items x)
{
    x.Alphabet_value = new int[x.name.Length];
    for (int c = 0; c < x.Alphabet_value.Length; c++)
    {
        string character = x.name.Substring(c, 1);
        character.ToLower();
        switch (character)
        {
            case "a":
                x.Alphabet_value[c] = 0;
                break;
            case "b":
                x.Alphabet_value[c] = 1;
                break;
            case "c":
                x.Alphabet_value[c] = 2;
                break;
            case "d":
                x.Alphabet_value[c] = 3;
                break;
            case "e":
                x.Alphabet_value[c] = 4;
                break;
            case "f":
                x.Alphabet_value[c] = 5;
                break;
            //To the end
        }
    }
}

我希望你能理解我想要说的 :D 谢谢 :) 在我开始这样做之前,我会尝试在互联网上查找一些信息,但我没有找到任何真正可以从数组中缩短多个字符串的信息。

这部分我认为我错了,但现在我看不出有什么问题:

 for (int h_pos = hold.Length - 1; h_pos > x; h_pos--) 
      if (hold [h_pos] != null)
      {
        hold [h_pos] = hold [h_pos - 1];                        
        hold [x] = item;
      }        

【问题讨论】:

    标签: c# arrays string short alphabetical-sort


    【解决方案1】:

    使用此代码。

            string str = "Tamil";
            List<char> list = str.ToList ();
            list = list.OrderBy(x => x.ToString()).ToList();
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
    

    【讨论】:

      【解决方案2】:

      首先,您真的认为拥有 +20 switch-case 是个好主意吗?

      字符已经编号,英文字母中的字符a-z对应Unicode中的字符97-122

      这个函数:

      void GetAlphabetaValue(Items x)
      {
        x.Alphabet_value = new int[x.name.Length];
        for (int c = 0; c < x.Alphabet_value.Length; c++) 
        {
            string character = x.name.Substring (c, 1);
            character.ToLower ();
            switch (character) 
            {
            case "a":
               x.Alphabet_value [c] = 0;
               break;  
             ///Etc... //Etc..... And end.  
            }
         }
      }    
      

      变成这样:

      void GetAlphabetaValue2(Items x)
      {
          var CharIntList = List<int>; 
          foreach (char ch in x.Name)
              CharIntList.Alphabet_value.Add(ch - 97);
         x.Alphabet_value = CharIntList.ToArray();
      }
      

      简单得多。此外,您的代码混乱、难以理解且格式错误。可能你是 C# 新手,所以你应该阅读一下你应该如何编写代码,这不是我做得很好,但其他人应该能够理解你的代码。

      关于你的问题,我认为你的意思是sort 而不是short(完全不同的东西)。还有为什么Items 是复数形式?是奇异的东西,那么应该是Item

      好吧,我不知道你的问题,但你可以将你的 GetListSetList() 函数替换为:

      private List<Items> GetListSetList(List<Items> items)
      {
          foreach (Items item in items)
          {
              GetAlphabetaValue(item);
              Array.Sort(item.Alphabet_value);
          }
          return items;
      }
      

      【讨论】:

      • 哇,谢谢.. 我在想有比 multi switch case 语句更好的方法:) 谢谢。
      • 你的代码应该是sortint[] Alphabet_value中的值吗?
      • 是的,最后一部分是我多次看的,但我没有注意到我可以使用类的指定部分(item.Alphabet_value),简称。我在想 Short 只能像 short(item) 那样工作; :)
      • @JaniLiekari 哪一个? SORTSHORT ?
      • 哦排序 :).. 但是当我这样做时,List&lt;Items&gt; GetListSetList(List&lt;Items&gt; items){ foreach (Items item in items) { GetAlphabetaValue (item); Array.Sort (item.Alphabet_value); } return items; }
      【解决方案3】:

      试试这样的

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  List<Items> items = Items.returnList("Food");
                  var groups = items.GroupBy(x => x.name.Substring(0,1)).ToList();
      
              }
          }
          public class Items
          {
              public static List<Items> items = new List<Items>();
              public string name { get; set; }
              public string type { get; set; }
      
      
              public static List<Items> returnList(string type)
              {
                  return items.Where(x => x.type == type).ToList();
              }
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多