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?