如何通过其属性删除对象列表中的多个对象

时间:2016-04-15 11:59:49

标签: c#

我又回来了另一个关于物体的问题。 请注意我一般只需要一个月的编程,所以如果这是一个愚蠢的问题,我很抱歉。

现在我正在做一些功课,让我设计一个图书馆课和一本书课。这个图书馆应该存放一些书籍。我应该用一些方法操纵这个对象列表。除了一个,我知道如何设计的大多数方法。我应该在这个列表中搜索所有具有特定作者的书籍并删除它们。我不知道如何处理这个问题。我可以通过书类的属性从列表中删除单个对象,但我不知道如何删除它的多个对象。

    class Library
    {
    public string Name { get; set; }
    public List<Book> list = new List<Book>();
    public List<Book> foundBooks = new List<Book>();

    public Library(string name)
    {
        this.Name = name;
    }
    //Library Methods
    public void AddBook(Book book)
    {
        list.Add(book);
    }

    public void PrintBookInfo(Book book)
    {
        Console.WriteLine("Title: "+ book.Title);
        Console.WriteLine("Author " +book.Author);
        Console.WriteLine("Publisher: "+ book.Publisher);
        Console.WriteLine("Release Date: " + book.ReleaseDate.ToString());
        Console.WriteLine("ISBN: " + book.ISBN);
        Console.WriteLine();
    }

    public void PrintAllBooks()
    {
        foreach (Book book in list)
        {
            PrintBookInfo(book);
        }
    }

    public Book SearchForBook(string name)
    {

        foreach (Book book in list)
        {
            if (book.Author == name)
            {
                PrintBookInfo(book);
                return book;         

            }

        }
        return null;
    }

    public void DeleteBook(Book book)// this is the method im using to delete objects but I don't know how to extend this.
    {
        list.Remove(book);
    }
}

这是我的书类

class Book
{
    //Properties
    public string Title { get; set; }
    public string Author { get; set; }
    public string Publisher { get; set; }  
    public DateTime ReleaseDate { get; set; }
    public int ISBN { get; set; }


    public Book(string Title, 
        string Author,
        string Publisher,
        DateTime ReleaseDate,
        int ISBN)
    {
        this.Title = Title;
        this.Author = Author;
        this.Publisher = Publisher;
        this.ReleaseDate = ReleaseDate;
        this.ISBN = ISBN;
    }
}

最后我的主要到目前为止:

        class Program
{
    static void Main(string[] args)
    {
        var library = new Library("Yishun Regional Library");
        var bookOne = new Book("Harry Potter", "JK Rowling", "UK", DateTime.Today, 1165);
        var bookTwo = new Book("Eragon", "Paolini", "Netherlands", DateTime.Today, 2565);
        var bookThree = new Book("Harry", "JK Rowling", "UK", DateTime.Today, 1145);

        library.AddBook(bookOne);
        library.AddBook(bookTwo);
        library.AddBook(bookThree);

        Console.WriteLine("Hello Welcome To Yishun Library. Take a look at the books Availabe.");
        Console.WriteLine();

        foreach (Book book in library.list)
        {
            library.PrintBookInfo(book);
        }

        Console.WriteLine();
        string name;

        Console.WriteLine("Enter name of author: ");
        name = Console.ReadLine();
        var bookFound = library.SearchForBook(name);

        library.DeleteBook(bookFound);
        Console.WriteLine("Current list of books: \n");

        library.PrintAllBooks();
    }

我认为实现此目的的一种方法是创建一个已找到的书籍数组,然后使用foreach方法删除书籍,但我不知道如何实现它。或者更确切地说,我很快发现你无法使用foreach来操纵数据。任何帮助将不胜感激! ]

3 个答案:

答案 0 :(得分:2)

诀窍是使用> javac Example.java > java -classpath ".;sqlite-jdbc-(VERSION).jar" Example # in Windows or > java -classpath ".:sqlite-jdbc-(VERSION).jar" Example # in Mac or Linux 循环而不是for一个循环向后

foreach

如果循环前进,方案更复杂:

  for (int i = list.Count - 1; i >= 0; --i)
      if (String.Equals(list[i].Author, "AuthorToDelete"))
          list.RemoveAt(i); 

或者只是打电话(在家庭作业中接受吗?)

  for (int i = 0; i < list.Count;)
      if (String.Equals(list[i].Author, "AuthorToDelete"))
          list.RemoveAt(i); 
      else
          i += 1;

答案 1 :(得分:1)

Library课程中:

public void DeleteBooksByAuthor(string authorName)
{
    list.RemoveAll(x => x.Author == authorName);
}

答案 2 :(得分:0)

您可以在课堂上使用以下代码。它也会处理列表对象的空检查

    public void DeleteBooksByAuthor(string authorName)
    {
        list?.RemoveAll(x => x?.Author == authorName);
    }