包含对象列表的多个列表

我有 2 个清单。每个列表由其中的对象组成,即对象列表。


List<Book> listBook= new List<Book>();

List<Pen> listPen = new List<Pen>();


class Book {

    string name;

    int id;


    Book() {

        this.name = name;

        this.id = id;

    }

}


class Pen {

    string name;

    int id;


    Pen() {

        this.name = name;

        this.id = id;

    }

}

listBook其中有 10 个对象,同样也listPen有 10 个对象。


我需要一个add方法,通过传递列表对象或带有详细信息的列表的引用,列表必须更新。我的意思是,单一方法,使用 list-pen 或 list-book 调用该方法应该分别向 list-pen 或 list-book 添加新对象。


感谢您的任何建议或帮助


慕桂英546537
浏览 104回答 3
3回答

BIG阳

您可以为此使用泛型。下面的addToList方法是一个通用方法,它接受任何类型的列表和对象来添加它。public static <T> void addToList(final List<T> list, final T toAdd) {&nbsp; &nbsp; list.add(toAdd);&nbsp; }&nbsp; public static void main(final String[] args) {&nbsp; &nbsp; final List<Book> listBook = new ArrayList<Book>();&nbsp; &nbsp; final List<Pen> listPen = new ArrayList<Pen>();&nbsp; &nbsp; addToList(listPen, new Pen());&nbsp; &nbsp; addToList(listBook, new Book());&nbsp; }注意(不可能):在您的代码中,您创建了 的对象List,而 List 是一个接口,因此您无法创建 List 的对象。相反,它应该是ArrayList().

慕标琳琳

如果您只寻找一种方法(即 Add()),这里是完整的代码。using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;namespace Rextester{&nbsp; &nbsp; public class Book&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string name;&nbsp; &nbsp; &nbsp; &nbsp; public int id;&nbsp; &nbsp; &nbsp; &nbsp; public Book(string name, int id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Pen&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string name;&nbsp; &nbsp; &nbsp; &nbsp; public int id;&nbsp; &nbsp; &nbsp; &nbsp; public Pen(string name, int id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static List<Book> listBook = new List<Book>();&nbsp; &nbsp; &nbsp; &nbsp; static List<Pen> listPen = new List<Pen>();&nbsp; &nbsp; &nbsp; &nbsp; public static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listBook.Add(new Book&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Book1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listBook.Add(new Book&nbsp; &nbsp; &nbsp; &nbsp; ("Book2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listPen.Add(new Pen&nbsp; &nbsp; &nbsp; &nbsp; ("Pen1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listPen.Add(new Pen("Pen2", 2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Testing with new book list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Book> newListBook = new List<Book>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newListBook.Add(new Book&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("Book3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newListBook.Add(new Book&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("Book4",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Add(newListBook);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in listBook)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(item.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Pen> newListPen = new List<Pen>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newListPen.Add(new Pen&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("Pen3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newListPen.Add(new Pen&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("Pen4",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Add(newListPen);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in listPen)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(item.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static void Add<T>(IEnumerable<T> obj)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in obj)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (item is Book)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listBook.Add(item as Book);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (item is Pen)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listPen.Add(item as Pen);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

跃然一笑

不确定我是否正确理解你的问题,所以我会根据你使用 C# 的假设提出一些解决方案:情况1:如果你只是想向列表中添加一个项目,那么就使用Ling。using System.Linq;List<Book> books = new List<Book>();Book myBook = new Book();books.Add(myBook);情况2:如果你想创建一个适用于任何数据类型的方法,那么编写一个模板public void MyAddMethod<T>(ref List<T> list, T item){&nbsp; &nbsp; list.Add(item);}List<Book> book = new List<Book>();Book myBook = new Book();MyAddMethod(ref books, myBook);这是我最好的选择。希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java