第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

包含對(duì)象列表的多個(gè)列表

包含對(duì)象列表的多個(gè)列表

慕桂英546537 2023-09-20 16:39:50
我有 2 個(gè)清單。每個(gè)列表由其中的對(duì)象組成,即對(duì)象列表。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 個(gè)對(duì)象,同樣也listPen有 10 個(gè)對(duì)象。我需要一個(gè)add方法,通過傳遞列表對(duì)象或帶有詳細(xì)信息的列表的引用,列表必須更新。我的意思是,單一方法,使用 list-pen 或 list-book 調(diào)用該方法應(yīng)該分別向 list-pen 或 list-book 添加新對(duì)象。感謝您的任何建議或幫助
查看完整描述

3 回答

?
BIG陽

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以為此使用泛型。下面的addToList方法是一個(gè)通用方法,它接受任何類型的列表和對(duì)象來添加它。


public static <T> void addToList(final List<T> list, final T toAdd) {


    list.add(toAdd);

  }


  public static void main(final String[] args) {

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

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


    addToList(listPen, new Pen());

    addToList(listBook, new Book());

  }

注意(不可能):在您的代碼中,您創(chuàng)建了 的對(duì)象List,而 List 是一個(gè)接口,因此您無法創(chuàng)建 List 的對(duì)象。相反,它應(yīng)該是ArrayList().


查看完整回答
反對(duì) 回復(fù) 2023-09-20
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

如果您只尋找一種方法(即 Add()),這里是完整的代碼。


using System.Collections.Generic;

using System.Linq;

using System.Text.RegularExpressions;



namespace Rextester

{


    public class Book

    {

        public string name;

        public int id;

        public Book(string name, int id)

        {

            this.name = name;

            this.id = id;

        }

    }


    public class Pen

    {

        public string name;

        public int id;

        public Pen(string name, int id)

        {

            this.name = name;

            this.id = id;

        }

    }

    public class Program

    {

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

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

        public static void Main(string[] args)

        {



            listBook.Add(new Book

            (

                 "Book1",

             1

            ));

            listBook.Add(new Book

        ("Book2",

              2

            ));




            listPen.Add(new Pen

        ("Pen1",

                 1

            ));

            listPen.Add(new Pen("Pen2", 2));




            //Testing with new book list


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

            newListBook.Add(new Book

            ("Book3",

                  3

            ));

            newListBook.Add(new Book

            ("Book4",

                  4

            ));


            Add(newListBook);


            foreach (var item in listBook)

            {

                Console.WriteLine(item.name);


            }



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

            newListPen.Add(new Pen

            ("Pen3",

                  3

            ));

            newListPen.Add(new Pen

            ("Pen4",

                  4

            ));


            Add(newListPen);


            foreach (var item in listPen)

            {

                Console.WriteLine(item.name);


            }


            Console.ReadLine();

        }


        public static void Add<T>(IEnumerable<T> obj)

        {

            foreach (var item in obj)

            {

                if (item is Book)

                {

                    listBook.Add(item as Book);


                }

                if (item is Pen)

                {

                    listPen.Add(item as Pen);


                }


            }

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-09-20
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

不確定我是否正確理解你的問題,所以我會(huì)根據(jù)你使用 C# 的假設(shè)提出一些解決方案:


情況1:如果你只是想向列表中添加一個(gè)項(xiàng)目,那么就使用Ling。


using System.Linq;


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

Book myBook = new Book();

books.Add(myBook);

情況2:如果你想創(chuàng)建一個(gè)適用于任何數(shù)據(jù)類型的方法,那么編寫一個(gè)模板


public void MyAddMethod<T>(ref List<T> list, T item)

{

    list.Add(item);

}


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

Book myBook = new Book();

MyAddMethod(ref books, myBook);

這是我最好的選擇。希望這可以幫助。


查看完整回答
反對(duì) 回復(fù) 2023-09-20
  • 3 回答
  • 0 關(guān)注
  • 135 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)