我试图找出如何将一个对象添加到一个arraylist然后返回它。 我的代码如下:
mediaTitleCollection = new ArrayList();
public BookMedia CreateBookTitle(string title, string subtitle, string edition, string author, string genre, int weight, int units, string isbn, int pages, int chapters)
{
mediaTitleCollection.Add(new BookMedia(title, subtitle, edition, author, genre, weight, units, isbn, pages, chapters));
// Return the object i have just added in mediaTitleCollection
}
我尝试了几种方法并搜索了半个小时,似乎无法找到解决问题的正确方法..
提前致谢。
答案 0 :(得分:3)
mediaTitleCollection = new ArrayList();
public BookMedia CreateBookTitle(string title, string subtitle, string edition, string author, string genre, int weight, int units, string isbn, int pages, int chapters)
{
BookMedia result=new BookMedia(title, subtitle, edition, author, genre, weight, units, isbn, pages, chapters);
mediaTitleCollection.Add(result);
// Return the object i have just added in mediaTitleCollection
return result;
}
答案 1 :(得分:2)
return mediaTitleCollection[mediaTitleCollection.Count-1];//After adding, it returns the last object(don't need to initialize a local scope variable)
答案 2 :(得分:0)
mediaTitleCollection = new ArrayList();
public BookMedia CreateBookTitle(string title, string subtitle, string edition, string author, string genre, int weight, int units, string isbn, int pages, int chapters)
{
BookMedia book = new BookMedia(title, subtitle, edition, author, genre, weight, units, isbn, pages, chapters);
mediaTitleCollection.Add(book );
return book;
// Return the object i have just added in mediaTitleCollection
}
答案 3 :(得分:0)
mediaTitleCollection = new ArrayList();
public BookMedia CreateBookTitle(string title, string subtitle, string edition, string author, string genre, int weight, int units, string isbn, int pages, int chapters)
{
BookMedia bm = new BookMedia(title, subtitle, edition, author, genre, weight, units, isbn, pages, chapters);
mediaTitleCollection.Add(bm);
// Return the object i have just added in mediaTitleCollection
return bm;
}