在Java中对arraylist的对象使用方法 公共课堂教材 { 私有字符串isbn,作者,区域; 私有整数长度; 公共图书(字符串isbn、字符串作者、字符串区域、整数长度) { 这个。isbn=isbn; this.author=作者; 这个。面积=面积; 这个。长度=长度; } 公共布尔isLong() { 如果(长度>500) 返回true; 其他的 返回false; } } 公共类藏书 { private ArrayList bookList=new ArrayList(); public BookCollection()引发异常 { 扫描仪文件扫描; 字符串单线; 字符串isbn,作者,区域; 整数长度; fileScan=新扫描仪(新文件(“books.txt”); while(fileScan.hasNext()) { isbn=fileScan.next(); author=fileScan.next(); area=fileScan.next(); length=fileScan.nextInt(); 增加(新书(isbn、作者、面积、长度); } } 公共类TestBookCollection { 公共静态void main(字符串[]args)引发IOException { BookCollection books=新建BookCollection(); 系统输出打印(书籍); } }
好的,这是相关的代码。我的项目是读取一个包含这些书籍信息的文本文件,并将它们放入书籍对象的arraylist中。我的问题是:我该如何处理isLong()在arraylist中对象的类书中找到的方法?该方法的要点是,如果一个对象的页面超过500页,它将返回true。如果没有,它将返回false。我只是对逻辑有点困惑,我以前从未真正使用过arraylist。您可以向BookCollection添加另一个方法:在Java中对arraylist的对象使用方法 公共课堂教材 { 私有字符串isbn,作者,区域; 私有整数长度; 公共图书(字符串isbn、字符串作者、字符串区域、整数长度) { 这个。isbn=isbn; this.author=作者; 这个。面积=面积; 这个。长度=长度; } 公共布尔isLong() { 如果(长度>500) 返回true; 其他的 返回false; } } 公共类藏书 { private ArrayList bookList=new ArrayList(); public BookCollection()引发异常 { 扫描仪文件扫描; 字符串单线; 字符串isbn,作者,区域; 整数长度; fileScan=新扫描仪(新文件(“books.txt”); while(fileScan.hasNext()) { isbn=fileScan.next(); author=fileScan.next(); area=fileScan.next(); length=fileScan.nextInt(); 增加(新书(isbn、作者、面积、长度); } } 公共类TestBookCollection { 公共静态void main(字符串[]args)引发IOException { BookCollection books=新建BookCollection(); 系统输出打印(书籍); } },java,class,arraylist,methods,Java,Class,Arraylist,Methods,好的,这是相关的代码。我的项目是读取一个包含这些书籍信息的文本文件,并将它们放入书籍对象的arraylist中。我的问题是:我该如何处理isLong()在arraylist中对象的类书中找到的方法?该方法的要点是,如果一个对象的页面超过500页,它将返回true。如果没有,它将返回false。我只是对逻辑有点困惑,我以前从未真正使用过arraylist。您可以向BookCollection添加另一个方法: public class Book { private String isbn, auth
public class Book
{
private String isbn, author, area;
private int length;
public Book(String isbn, String author, String area, int length)
{
this.isbn=isbn;
this.author=author;
this.area=area;
this.length=length;
}
public boolean isLong()
{
if (length>500)
return true;
else
return false;
}
}
public class BookCollection
{
private ArrayList<Book> bookList = new ArrayList<Book>();
public BookCollection()throws IOException
{
Scanner fileScan;
String oneLine;
String isbn, author, area;
int length;
fileScan = new Scanner (new File("books.txt"));
while (fileScan.hasNext())
{
isbn=fileScan.next();
author=fileScan.next();
area=fileScan.next();
length=fileScan.nextInt();
bookList.add(new Book(isbn, author, area, length));
}
}
public class TestBookCollection
{
public static void main (String[] args)throws IOException
{
BookCollection books = new BookCollection();
System.out.println(books);
}
}
上面使用了Java中所谓的“for each”循环样式,您可以使用它来循环每个数组/集合,而不是(int i=0;i 500;for)的“old school”
B) 从文件中读取内容并不是直接在构造函数中执行的操作。相反,您应该为此创建一个专用方法,然后可以在构造函数中调用该方法,而不是将创建图书列表的逻辑放在构造函数中,您应该创建一个名为getBookCollection()的方法返回一个数组列表
books.printLongBooks()
您可以使用for
循环遍历ArrayList
的元素,并对其每个对象调用该方法
for(Book book: bookList){
if(book.isLong()){
//logic
} else {
//logic
}
}
您有一个列表
,其中列表
中的每个索引都包含一本书籍
。使用List.get(index)
方法在索引处获取给定列表
中的对象。例如:bookList.get(0)
获取索引0处的书籍
。一旦拥有该对象
,您就可以正常使用它了
我假设您有办法在BookCollection
中获取bookList
,以及书籍的名称
for(Book book : bookList){
boolean isLong = book.isLong();
}
publicstaticvoidmain(字符串[]args){
BookCollection books=newbookcollection();//创建一个新的BookCollection
List booksInCollection=books.getBooksList();//获取BookCollection中的书籍
//把书中的每一本通读一遍
对于(int index=0;index
首先(如果您不想使用流等),您需要从ArrayList获取对象。您可以使用ArrayList.get(int index)
然后调用该方法或使用for each循环:
public static void main(String[] args){
BookCollection books = new BookCollection(); // Make a new BookCollection
List<Book> booksInCollection = books.getBooksList(); // Get the Books inside BookCollection
// Go through each book in booksInCollection
for(int index = 0; index < booksInCollection.size(); index++){
// Get the Book Object at index
Book currentBook = booksInCollection.get(index);
if(currentBook.isLong()){
System.out.println(currentBook.getName() + " is long!");
}
}
}
问题是您无法从main访问私有字段(bookList)。有几种方法可以使其工作
- 在BookCollection中创建公共方法
- 将字段更改为public,然后直接访问它
books.bookList
- 在BookCollection中为您的书单创建一个getter
- 使BookCollection扩展ArrayList
您在这里所做的是创建一个装饰器(BookCollection
),它用附加功能(在本例中,是一个基于文件预填充的构造函数)包装一个ArrayList
),这为您提供了两个如何使用列表中的书籍的选项:
通过getter访问列表
,然后将其计算出来。使语句较长,但这对于少量使用是可以接受的:
for(Book book : bookList) {
System.out.println(book.isLong());
}
更常见的是,您将在decorator中创建提供公共功能的方法,如:
if(books.getBooks().get(index).isLong()) {
//The book at index is long
}
然后从业务逻辑中调用decorator方法,甚至可以创建更复杂的方法,比如GhostCat提供的方法
thelist.get(theindex.isLong)()
for(Book book : bookList) {
System.out.println(book.isLong());
}
if(books.getBooks().get(index).isLong()) {
//The book at index is long
}
public boolean isLong(int index) {
return bookList.get(index).isLong();
}