简体   繁体   English

Java:如何计算arraylist中的出现次数,然后将其输出到toString中以打印输出?

[英]Java: How to count occurrences in an arraylist, then get it to print that output in a toString?

So I am making a library book simulation, the current part I am stuck on is trying to find the number of books on loan and to then for this to be printed out when the toString method is called. 因此,我正在模拟一个图书馆的书籍,目前我所坚持的部分是尝试查找借出的书籍数量,然后在调用toString方法时将其打印出来。

public class Biblio{
    private ArrayList<Book> collection;
    private int loanCount;

The status of the book is of type enum 这本书的状态为枚举类型

public enum Status {LOAN, REFERENCE, AVAILABLE};

the code I am stuck on: 我坚持的代码:

public int numberOfBooksOnLoan(){
    int loanCount= 0;
    for(Book onLoan: collection){
        if(onLoan.getBookStatus()== Status.LOAN){
            loanCount++;    
        }
    }
    return loanCount;
}


      @Override
public String toString(){
    StringBuilder string= new StringBuilder();
    string.append("total number of books: ").append(bookCollection.size()).append("\n");
    string.append("number of loaned books: ").append(loanCount).append("\n");
    return string.toString();
}

both numberOfBooksOnLoan and the toString methods are in the Biblio class numberOfBooksOnLoantoString方法都在Biblio类中

When I use the toString method, the value 0 is printed, even when books are on loan, in contrast to the getLoanCount method (also in this class) which will print the required value. 当我使用toString方法时,即使将书借来,也将打印0值,而getLoanCount方法(也在此类中)将打印所需的值,这getLoanCount相反。

My question is, how can I go through an arraylist and count all the books which are on loan then have this value stored in a variable so I can use it in the toString? 我的问题是,我该如何遍历arraylist并计算所有借出的书,然后将此值存储在变量中,以便可以在toString中使用它? Or more generally, how do you go through an arraylist and count all objects with a specific variable in common? 或更笼统地说,您如何遍历arraylist并计算具有共同特定变量的所有对象?

In case this is part of the problem, I have the warning 'local variable hides a field' in reference to 如果这是问题的一部分,我将警告“本地变量隐藏字段”

int loanCount = 0;

Do you have a instance variable also called loanCount? 您是否有一个实例变量也称为loanCount? If the answer is yes try removing the instance one and change toString to call the method: 如果答案是肯定的,请尝试删除实例一并更改为toString以调用该方法:

@Override
public String toString(){
    StringBuilder string= new StringBuilder();
    string.append("total number of books: ").append(bookCollection.size()).append("\n");
    string.append("number of loaned books: ").append(numberOfBooksOnLoan()).append("\n");
    return string.toString();
}
public int numberOfBooksOnLoan(){
 int loanCount= 0;
 for(Book onLoan: collection){
    if(onLoan.getBookStatus()== Status.LOAN){
        loanCount++;    
    }
 }
 return loanCount;
}

You are creating a local variable in numberOfBooksOnLoan() but in your toString() method your are using the class attribute loanCount . 您在numberOfBooksOnLoan()中创建一个局部变量,但是在您的toString()方法中,您正在使用类属性loanCount

Just remove int from int loanCount= 0; 只需从int loanCount= 0;删除int int loanCount= 0; in the numberOfBooksOnLoan() so you will use the class attribute instead of the local variable. numberOfBooksOnLoan()因此您将使用class属性而不是局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM