创建一个Arraylist,然后向里面添加一个Loan对象
时间: 2023-07-19 22:09:07 浏览: 77
如何向WebServices中传送多个自定义类对象
5星 · 资源好评率100%
好的,以下是Java语言的示例代码:
首先,需要创建一个Loan类:
```java
public class Loan {
private String borrower;
private double amount;
public Loan(String borrower, double amount) {
this.borrower = borrower;
this.amount = amount;
}
public String getBorrower() {
return borrower;
}
public double getAmount() {
return amount;
}
}
```
然后,在主函数中创建ArrayList并添加一个Loan对象:
```java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Loan> loanList = new ArrayList<>();
Loan loan = new Loan("Alice", 1000.0);
loanList.add(loan);
}
}
```
这样就创建了一个ArrayList,并向其中添加了一个Loan对象。
阅读全文