我已经建立了我的班书目一个ArrayList,但我需要它在另一个类打印。
我想我可能把一个ArrayList的印刷进入一个方法,所以那么它可以从不同的类调用;如果我错了,虽然,还有什么可以做我做到这一点?
下面是我的code,以供参考。
进口的java.util.ArrayList;公共类书目{公共静态无效的主要(字串[] args){ 书=新的图书(); a.setTitle(随机); a.setAuthor(克雷格·罗伯逊); a.setBookID(1847398812); a.setonLoan(假); a.setNumberofLoans(3); 图书B =新的图书(); b.setTitle(最后的避难所); b.setAuthor(克雷格·罗伯逊); b.setBookID(1471127753); b.setonLoan(假); b.setNumberofLoans(2); 图书C =新的图书(); c.setTitle(未唱鸟); c.setAuthor(亚历克斯灰色); c.setBookID(0751548278); c.setonLoan(真); c.setNumberofLoans(7); ArrayList的<图书>书目=新的ArrayList<图书>(); BookList.add(一); BookList.add(二); BookList.add(C); 诠释计数= BookList.size(); 的System.out.println(伯爵:+计数); 的System.out.println(); 对于(图书D:书目){ 的System.out.println(D); } }}要顺应这一点,我在我的图书类中的toString()方法,这样我就可以打印所有对象。
另外一个问题:我如何将打印每个对象的只是标题和作者
解决方案创建的静态在图书类的方法将处理的显示
公共静态无效showBooks(ArrayList的<图书>书){ 对于(图书D:书籍){ 的System.out.println(D); //调用每个Book对象的'的toString'方法}和调用它这种方式从主要方法:
的ArrayList<图书>书目=新的ArrayList<图书>();BookList.add(一);BookList.add(二);BookList.add(C);Book.showBooks(书目); //打印的书籍,一个一个
我将如何打印每个对象的只是标题和作者?
您可以选择打印标题和作者仅在重写 的toString 你的图书类的方法:
@覆盖公共字符串的toString(){ //'作家'和; '称号'是'书'类的成员 回归作者:+ +作家 - 标题:+称号;}I've built an Arraylist in My class 'BookList', but I need it to print in another class.
I think I may have to turn the printing of an Arraylist into a method, so then it can be called from a different class; if I am wrong though, what else could I do to accomplish this?
Below is my code, for reference.
import java.util.ArrayList; public class BookList { public static void main (String[] args){ Book a = new Book(); a.setTitle("Random "); a.setAuthor("Craig Robertson"); a.setBookID("1847398812"); a.setonLoan(false); a.setNumberofLoans(3); Book b = new Book(); b.setTitle("The Last Refuge"); b.setAuthor("Craig Robertson"); b.setBookID("1471127753"); b.setonLoan(false); b.setNumberofLoans(2); Book c = new Book(); c.setTitle("The Bird That Did Not Sing"); c.setAuthor("Alex Gray"); c.setBookID("0751548278"); c.setonLoan(true); c.setNumberofLoans(7); ArrayList<Book> BookList = new ArrayList<Book>(); BookList.add(a); BookList.add(b); BookList.add(c); int count = BookList.size(); System.out.println("Count: " + count); System.out.println(""); for (Book d : BookList){ System.out.println(d); } } }To go with this, I have the toString() method in my 'Book' class, so I could print all objects.
An additional question: How would I print just the title and author of each object?
解决方案Create a static method in your Book class that will handle the display:
public static void showBooks(ArrayList<Book> books) { for (Book d : Books){ System.out.println(d); // calls the 'toString' method on each Book object }And call it this way from the main method:
ArrayList<Book> BookList = new ArrayList<Book>(); BookList.add(a); BookList.add(b); BookList.add(c); Book.showBooks(BookList); // prints the books, one by one
How would I print just the title and author of each object?
You can choose to print the title and author only in the overriden toString method of your Book class:
@Override public String toString() { // 'author' & 'title' are members of the 'Book' class return "Author: " + author + "- Title: " + title; }
更多推荐
在另一个类中创建一个方法来打印ArrayList中,然后调用它
发布评论