Java编程:创建一个Book类包含两个String类型私有属性name(书名)和publish(出
Java编程:创建一个Book类包含两个String类型私有属性name(书名)和publish(出版社),添加适当的get/set方法;使用ArrayList创建两个C...
Java编程:创建一个Book类包含两个String类型私有属性name(书名)和publish(出版社),添加适当的get/set方法;使用ArrayList创建两个Collection对象c1和c2,c1存放3本书分别是(“数据结构”,“高等教育”),(“操作系统”,“电子工业出版社”),(“Java”,“电子工业出版社”),c2存放2本书分别是(“操作系统”,“电子工业出版社”)和(“数据库系统”,“清华大学出版社”);输出c1中是否包含书名为Java,出版社为机械工业出版社的图书信息;将c1和c2的差集存放到c1,输出差以后c1的元素个数,并使用迭代器iterator输出c1的内容
展开
1个回答
展开全部
我写了一个,方法可能有点麻烦,但是要求实现了
book类:
public class Book {
private String name;
private String publish;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public Book(String name, String publish) {
super();
this.name = name;
this.publish = publish;
}
public Book() {
}
}
测试类:
public class TestBook {
public static void main(String[] args) {
Book book1=new Book("数据结构", "高等教育");
Book book2=new Book("操作系统", "电子工业出版社");
Book book3=new Book("Java", "电子工业出版社");
Book book4=new Book("数据结构", "清华大学出版社");
Collection<Book> c1=new ArrayList<>();
Collection<Book> c2=new ArrayList<>();
Collection<Book> c3=new ArrayList<>();
c1.add(book1);
c1.add(book2);
c1.add(book3);
c2.add(book2);
c2.add(book4);
boolean cont=false;
for(Book book:c1){
if(book.getName().equals("Java")&&book.getPublish().equals("机械工业出版社")){
cont=true;
}
}
System.out.println(cont);
for(Book book02:c2){
boolean a=true;
for(Book book01:c1){
if(book01.getName().equals(book02.getName())&&book01.getPublish().equals(book02.getPublish())){
a=false;
}
}
if(a){
c3.add(book02);
}
}
for(Book b:c3){
c1.add(b);
}
Iterator<Book> iterator=c1.iterator();
while(iterator.hasNext()){
Book b=iterator.next();
System.out.println("书名:"+b.getName());
System.out.println("出版社:"+b.getPublish());
}
}
}
book类:
public class Book {
private String name;
private String publish;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public Book(String name, String publish) {
super();
this.name = name;
this.publish = publish;
}
public Book() {
}
}
测试类:
public class TestBook {
public static void main(String[] args) {
Book book1=new Book("数据结构", "高等教育");
Book book2=new Book("操作系统", "电子工业出版社");
Book book3=new Book("Java", "电子工业出版社");
Book book4=new Book("数据结构", "清华大学出版社");
Collection<Book> c1=new ArrayList<>();
Collection<Book> c2=new ArrayList<>();
Collection<Book> c3=new ArrayList<>();
c1.add(book1);
c1.add(book2);
c1.add(book3);
c2.add(book2);
c2.add(book4);
boolean cont=false;
for(Book book:c1){
if(book.getName().equals("Java")&&book.getPublish().equals("机械工业出版社")){
cont=true;
}
}
System.out.println(cont);
for(Book book02:c2){
boolean a=true;
for(Book book01:c1){
if(book01.getName().equals(book02.getName())&&book01.getPublish().equals(book02.getPublish())){
a=false;
}
}
if(a){
c3.add(book02);
}
}
for(Book b:c3){
c1.add(b);
}
Iterator<Book> iterator=c1.iterator();
while(iterator.hasNext()){
Book b=iterator.next();
System.out.println("书名:"+b.getName());
System.out.println("出版社:"+b.getPublish());
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询