创建一个打印 ArrayList 的方法,然后在另一个类中调用它 [英] Creating a method to print ArrayList, then calling it in another class
问题描述
我已经在我的类BookList"中构建了一个 Arraylist,但我需要在另一个类中打印它.
我想我可能不得不把一个Arraylist的打印变成一个方法,这样才能从不同的类中调用它;如果我错了,我还能做些什么来实现这一目标?
以下是我的代码,供参考.
import java.util.ArrayList;公共类书单{公共静态无效主(字符串 [] args){Book a = new Book();a.setTitle("随机");a.setAuthor("克雷格罗伯逊");a.setBookID("1847398812");a.setonLoan(false);a.setNumberofLoans(3);书 b = 新书();b.setTitle("最后的避难所");b.setAuthor("克雷格罗伯逊");b.setBookID("1471127753");b.setonLoan(false);b.setNumberofLoans(2);书 c = 新书();c.setTitle("不唱歌的鸟");c.setAuthor("Alex Gray");c.setBookID("0751548278");c.setonLoan(真);c.setNumberofLoans(7);ArrayListBookList = new ArrayList();BookList.add(a);BookList.add(b);BookList.add(c);int count = BookList.size();System.out.println("计数:" + count);System.out.println("");for (Book d : BookList){System.out.println(d);}}}
为此,我的Book"类中有 toString() 方法,因此我可以打印所有对象.
另一个问题:如何只打印每个对象的标题和作者?
在您的 Book
类中创建一个 static 方法来处理显示:
public static void showBooks(ArrayList books) {对于(书籍 d:书籍){System.out.println(d);//在每个 Book 对象上调用 'toString' 方法}
并从主方法中这样调用它:
ArrayListBookList = new ArrayList();BookList.add(a);BookList.add(b);BookList.add(c);Book.showBooks(BookList);//一本一本地打印书籍
<块引用>
如何只打印每个对象的标题和作者?
你可以选择只在你的overriden
课:toString
方法中打印title
和author
>Book
@Override公共字符串 toString() {//'作者' &'title' 是 'Book' 类的成员返回作者:"+作者+-标题:"+标题;}
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 的方法,然后在另一个类中调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!