gpt4 book ai didi

java - 从数组列表打印

转载 作者:行者123 更新时间:2023-12-02 06:28:20 26 4
gpt4 key购买 nike

我需要能够从数组列表中打印出所有有贷款的学生。

指金额大于 0 的所有学生。

我已经可以打印所有学生以及他们拥有多少钱..但我不知道如何让它只打印有贷款的学生。

这是当前代码

public class LoanCompany
{
private ArrayList<StudentLoan> loans;
/**
* Constructor for objects of class LoanCompany
*/
public LoanCompany()
{
loans = new ArrayList<StudentLoan>();
}
/**
* add a loan amount and ID.
*/
public void addLoan(String ID,int Amount)
{
StudentLoan newSLoan= new StudentLoan(ID,Amount);
loans.add(newSLoan);
}
/**
* Return amount of loans
*/
public int getNumberOfLoans()
{
return loans.size();
}
public void printLoanDitails()
{
System.out.println("Loan Summery: ");
int index=0;
for(StudentLoan loan : loans) {
System.out.print(index + " : ");
loan.printDetails();
index++;
}
System.out.println();
}
/**
* Remove index.
*/
public void removeLoan(int index)
{
if(validIndex(index)) {
loans.remove(index);
}
}
/**
* Valid index... validates the index.
*/
private boolean validIndex(int index)
{
boolean valid;
if(index < 0) {
System.out.println("Index must be 0 or a positive number");
valid = false;
}
else if(index >= loans.size()) {
System.out.println("Index is too large.");
valid = false;
}
else {
valid = true;
}
return valid;
}
/**
* PayOff method, pays off the student loan.
*/
public void payOff(int index, int Amount)
{
if(validIndex(index)) {
StudentLoan student = loans.get(index);
student.payOff(Amount);
}
else {
System.out.println("Invalid Index.");
}
}
/**
* Prints outstanding loans.
*/
public void printOutstandingLoans()
{
System.out.println("Loan Summery: ");
for(StudentLoan loan : loans ) {
int index=0;
if (loan.getAmount() > 0) {
System.out.print(index + " : ");
loan.printDetails();
index++;
}
}
System.out.println();
}
public void removeClearedLoans() {
for(StudentLoan loan : loans ) {
int index=0;
if (loan.getAmount() == 0) {
loans.remove(loan);
System.out.print( "Cleared Loans Removed ");
}
index++;
}
}
}

最佳答案

我假设 StudentLoan 有一个字段 public int LoanAmount 那么你可以这样做:

for(StudentLoan loan : loans) {
int index=0;
if (loans.loanAmount > 0) {
System.out.print(index + " : ");
loan.printDetails();
index++;
}
}

这样,只会打印 loanAmount > 0 的贷款。

如果字段 loanAmountprivate,您只需为该字段实现一个 getter 方法并将条件更改为:

if (loan.getloanAmount() > 0) {

编辑

回答评论中的问题:

要删除 loamAmount 为 0 的所有贷款,我们只需向 foreach 循环添加另一个 if 即可:

for(StudentLoan loan : loans) {
int index=0;

// print loans with amount > 0

if (loan.loanAmount > 0) {
System.out.print(index + " : ");
loan.printDetails();
index++;
}

// delete loans with amount = 0

if (loan.loanAmount == 0) {
loans.remove(loan) // UNSAFE! see edit below
 }
}

编辑

在迭代集合时使用 .remove 是不安全的。应该使用迭代器来完成,如下所示:

import java.util.Iterator // add to imports

Iterator<StudentLoan> i = loans.iterator();

while (i.hasNext()) {
StudentLoan loan = i.next();
if (loan.getAmount() == 0) {
i.remove();
}
}

关于java - 从数组列表打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20306023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com