使用属性获取最长的字符串

时间:2018-05-01 14:28:10

标签: c#

我有以下代码:

public string Longest
{
    get
    {
        int min = int.MinValue;
        string longest = "";
        for (Node i = Head; i != null; i = i.Next)
        {
            if (i.Text.Length > min)
            {
                longest = i.Text.Length.ToString();
            }

            return longest;
        }

        return longest;
    }
}

问题是我有这些字符串:

List text = new List();
text.Add("Petar");
text.Add("AHS");
text.Add("Google");
text.Add("Me");

当我试用这个属性时,它说最长的字符串是5但不是真的,最长的字符串是6。我试图找出问题所在,但我找不到它。

2 个答案:

答案 0 :(得分:2)

您的代码有几个问题:

  • 长度可以至少为0,因此您不需要使用int.MinValue
  • 您将在第一次迭代时返回
  • 找到更长的值后,您没有更新min
  • 您将返回字符串的长度,而不是字符串本身

您的代码应如下所示:

public string Longest
{
    get
    {
        int longestLength = 0;
        string longestWord = string.Empty;

        for (Node i = Head; i != null; i = i.Next)
        {
            if (i.Text.Length > longestLength)
            {
                longestLength = i.Text.Length;
                longestWord = i.Text;
            }
        }

        return longestWord;
    }
}

如果您要返回的是最大长度而不是具有最大长度的单词,则您的属性被错误地命名和输入,它应该是这样的:

public int MaximumLength
{
    get
    {
        int maximumLength = 0;

        for (Node i = Head; i != null; i = i.Next)
        {
            if (i.Text.Length > maximumLength)
            {
                maximumLength  = i.Text.Length;
            }
        }

        return maximumLength;
    }
}

答案 1 :(得分:0)

如果您有IEnumerable<string>,请执行以下操作

var list = new List<string>();
list.Add("AAA");
list.Add("AAAAA");
list.Add("A");
list.Add("AAAA");
list.Add("AAAAAA");
list.Add("AA");

// max has the longest string
var max = list.Aggregate(string.Empty, 
          (bookmark, item) => item.Length>bookmark.Length ? item : bookmark);

或使用循环

string max = string.Empty;
int length=0;
foreach(var item in list)
{
    if(item.Length>length)
    {
        max = item;
        length = item.Length;
    }
}

但是看起来你有一个链表,我在下面重新创建了一个骨架:

public class Node 
{
    public Node(string text)
    {
        this.Text = text;
        this.Head = this;
    }
    public Node(Node parent, string text): this(text)
    {
        if(parent!=null)
        {
            parent.Next = this;
            this.Head = parent.Head;
        }
    }
    public Node Head { get; }
    public Node Next { get; set; }
    public string Text { get; }
    public Node Add(string text) => new Node(this, text);
}

并找到带循环的最长字符串

var list = new Node("AAA");
list = list.Add("AAAAA");
list = list.Add("A");
list = list.Add("AAAA");
list = list.Add("AAAAAA");
list = list.Add("AA");


string max = list.Text;
int length = max.Length;
for(Node node = list.Head; node != null; node = node.Next)
{
    if(node.Text.Length > length)
    {
        max = node.Text;
        length= node.Text.Length;
    }
}

// max has the longest string

修改1

我通过将循环代码移动到方法中来获取链接列表并使其成为IEnumerable<string>

public class Node : IEnumerable<string>
{
    public Node(string text)
    {
        this.Text = text;
        this.Head = this;
    }
    public Node(Node parent, string text) : this(text)
    {
        if(parent!=null)
        {
            parent.Next = this;
            this.Head = parent.Head;
        }
    }
    public Node Head { get; }
    public Node Next { get; set; }
    public string Text { get; }
    public Node Add(string text) => new Node(this, text);

    public IEnumerator<string> GetEnumerator()
    {
        // Loop through the list, starting from head to end
        for(Node node = Head; node != null; node = node.Next)
        {
            yield return node.Text;
        }
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 
}

现在我可以使用一个LINQ语句

var list = new Node("AAA");
list = list.Add("AAAAA");
list = list.Add("A");
list = list.Add("AAAA");
list = list.Add("AAAAAA");
list = list.Add("AA");


// max has the longest string
var max = list.Aggregate(string.Empty, 
          (bookmark, item) => item.Length>bookmark.Length ? item : bookmark);