有没有更有效的方法来比较字符串?

时间:2015-11-16 18:58:40

标签: c# loops compare

我正在创建一个程序,在array中存储3个电影对象。作为其中的一部分,有一种方法可以搜索具有由用户提供的相同2个演员的电影。对象中的实际actor只是存储为变量(_actor1_actor2)。这是我的代码:

static void sameActors(string actor1, string actor2)
    {
        foreach (Film i in filmLibrary)
        {
            //check if both actors are in the film
            if ((i.getActor1() == actor1 && i.getActor2() == actor2) || (i.getActor1() == actor2 && i.getActor2() == actor1))
            {
                foreach (Film j in filmLibrary)
                {
                    //makes sure that it does not compare to itself
                    if (i.getName() != j.getName())
                    {
                        //checks if films have same actors
                        if ((actor1.Equals(j.getActor1()) && actor2.Equals(j.getActor2())) || (actor2.Equals(j.getActor1()) && actor2.Equals(j.getActor2())))
                        {
                            Console.WriteLine(i.getName() + " and " + j.getName() + " both share the same actors.");
                        }
                    }
                }
            }
        }
        menu();
    }

代码完成了这项工作,但是对我来说,如果你第一次看代码,还有很多想法要做。有更有效的方法吗?

此外,当执行此代码时,它将进行两次比较,因此一旦它将说“电影1和电影2具有相同的演员。”然后它会说“电影2和电影1有相同的演员。“防止这种情况的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

我无法想象getActor1()的作用。为什么函数名中会有固定数字?

我将采用的方法是使用Dictionary<>Set<>将其存储在支持更快查找的集合中。我会将演员存储在那里,然后搜索速度会更快。它有额外的好处,它可以存储任意数量的演员(因为你永远不知道电影可能有多少演员)。

但是,再次,很难知道你为什么这样做,所以我不知道这是否符合你的要求。

答案 1 :(得分:0)

我将详细说明我认为乔纳森·伍德的去向。我将包含一些代码,这些代码可以为您的数据提供更好的模型,然后还包含使用 LINQ 的解决方案。对于初学者,我认为你的演员应该作为你电影中的一个集合存在。通过这种方式,您不会限制数据,然后您可以更加优雅地使用数据。

在这个例子中,我定义了我的电影对象,种子4电影,然后创建一个基于字典的索引的电影到演员相交的电影,排除自己。最后,我使用更多LINQ格式化电影来报告列表。在这个例子中,每部电影的演员数量没有限制,每部电影都可以与0部或其他电影相关。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Film> films = new List<Film>() {
                new Film() {
                    Title = "Terminator",
                    Actors = new List<string>() {
                        "Arnold Schwarzenegger",
                        "Michael Biehn",
                        "Linda Hamilton"
                    }
                },
                new Film() {
                    Title = "Aliens",
                    Actors = new List<string>() {
                        "Sigourney Weaver",
                        "Carrie Henn",
                        "Michael Biehn"
                    }
                },
                new Film() {
                    Title = "Avatar",
                    Actors = new List<string>() {
                        "Sam Worthington",
                        "Zoe Saldana",
                        "Sigourney Weaver"
                    }
                },
                new Film() {
                    Title = "Star Wars",
                    Actors = new List<string>() {
                        "Mark Hamill",
                        "Harrison Ford",
                        "Carrie Fisher"
                    }
                }
            };

            var filmIndex = films.ToDictionary(
                f => f.Title, 
                m => films
                    .Where(f => f.Title != m.Title && 
                        f.Actors
                        .Intersect(m.Actors)
                        .Any())
                    .Select(t => t.Title));

            foreach (var film in filmIndex)
            {
                Console.WriteLine(string.Format("Movie: {0} has same actors as {1}.", 
                    film.Key, 
                    film.Value.Any() ? film.Value.Aggregate((i, j) => i + ", " + j) : "no other film"));
            }

            Console.ReadKey();
        }
    }

    class Film
    {
        public string Title { get; set; }
        public IEnumerable<string> Actors { get; set; }
    }
}