Java 具有对象列表的多个列表
我有两张清单。每个列表由其中的对象组成,即对象列表Java 具有对象列表的多个列表,java,c#,Java,C#,我有两张清单。每个列表由其中的对象组成,即对象列表 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
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;
}
}
List listBook=new List();
List listPen=新列表();
课堂用书{
字符串名;
int-id;
书(){
this.name=名称;
this.id=id;
}
}
班笔{
字符串名;
int-id;
笔(){
this.name=名称;
this.id=id;
}
}
listBook
中有10个对象,与listPen
相同,它有10个对象
我需要一个add
方法,通过传递列表对象的引用或带有详细信息的列表,列表必须得到更新。
我的意思是,单个方法,使用list pen或list book调用该方法应该分别向list pen或list book添加新对象
感谢您提供的任何建议或帮助,您可以使用泛型进行此操作。如下所示,addToList方法是一种通用方法,它接受任何类型的列表和对象来添加它
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());
}
公共静态无效添加列表(最终列表,最终添加){
列表。添加(toAdd);
}
公共静态void main(最终字符串[]args){
最终列表listBook=new ArrayList();
最终列表listPen=新的ArrayList();
addToList(列表笔,新笔());
addToList(列表簿,新书());
}
注意(毫无疑问):在您的代码中,您已经创建了
列表的对象,而列表是一个接口,所以您不能创建列表的对象。相反,它应该是ArrayList()
的,我不确定我是否正确理解了您的问题,因此我将根据您使用C的假设提出一些解决方案:
案例1:如果您只是想将一个项目添加到列表中,那么使用Ling
using System.Linq;
List<Book> books = new List<Book>();
Book myBook = new Book();
books.Add(myBook);
使用System.Linq;
列表书籍=新列表();
Book myBook=新书();
books.Add(myBook);
案例2:如果您想创建一个可以处理任何数据类型的方法,那么就编写一个模板
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);
public void MyAddMethod(参考列表,T项)
{
列表。添加(项目);
}
列表簿=新列表();
Book myBook=新书();
MyAddMethod(参考书,myBook);
那是我最好的选择。希望这有帮助。如果您只寻找一种方法(即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);
}
}
}
}
}
使用System.Collections.Generic;
使用System.Linq;
使用System.Text.RegularExpressions;
名称空间测试仪
{
公共课堂用书
{
公共字符串名称;
公共int id;
公共图书(字符串名称,int-id)
{
this.name=名称;
this.id=id;
}
}
公共类钢笔
{
公共字符串名称;
公共int id;
公共笔(字符串名称,int-id)
{
this.name=名称;
this.id=id;
}
}
公共课程
{
静态列表listBook=新列表();
静态列表listPen=新列表();
公共静态void Main(字符串[]args)
{
添加(新书)
(
“第一册”,
1.
));
添加(新书)
(“第二册”,
2.
));
添加(新笔)
(“第1笔”,
1.
));
添加(新笔(“笔2”,2));
//使用新书列表进行测试
List newListBook=newlist();
添加(新书本)
(“第三册”,
3.
));
添加(新书本)
(“第四册”,
4.
));
添加(newListBook);
foreach(列表簿中的var项)
{
Console.WriteLine(项目名称);
}
List newListPen=新列表();
添加(新笔)
(“第三笔”,
3.
));
添加(新笔)
(“第四笔”,
4.
));
添加(newListPen);
foreach(listPen中的变量项)
{
Console.WriteLine(项目名称);
}
Console.ReadLine();
}
公共静态无效添加(IEnumerable obj)
{
foreach(obj中的var项目)
{
如果(项目为书本)
{
listBook.Add(项作为Book);
}
如果(项目为钢笔)
{
添加(项目作为笔);
}
}
}
}
}
请不要格式化代码,如果有任何逻辑,请帮助我谢谢格式化谢谢它工作得很好,很抱歉我无法投票,因为我的声誉没有达到要求的限度。你能详细解释一下吗?