我有三个类:Account.java
、Transaction.java
和 StudentPrint.java
(最后一个是主类) 。我需要创建一个链接列表,其中包含主类中的 Account 类和 Transaction 类的列表:StudentPrint。另外,我需要在 Account 类中列出最后 6 笔交易。
可能还有其他方法可以做到这一点,但我的要求是按照所描述的方式进行。我有 3 个类,其中一个是我有 main 方法的类。我需要一个账户类,一个交易类。在类(class)帐户中,我需要有学生的属性,在交易类(class)中,需要有交易类型(弹出、打印)、交易金额、交易数据和时间,并且我需要有一个链接列表,以便在类(class)帐户中我可以有一个方法来保存学生最近进行的 6 笔交易。
最佳答案
根据我从您提供的详细信息中了解到的情况,我假设您正在寻找的是管理最后 6 笔交易的数据结构。
其中一个是针对这个问题而设计的,称为“固定大小的循环队列”。
查看这 2 个 Stack Overflow 链接以获取实现示例或其他想法:
Is there a fixed sized queue which removes excessive elements?
Size-limited queue that holds last N elements in Java
.
应该这样实现:
public class Account {
private int studentId;
...
CircularFifoQueue<Integer> lastTransactions = new CircularFifoQueue<Integer>(6);
}
public class StudentPrint {
LinkedList<Account> accountList = new LinkedList<Account>();
}
关于java - 如何创建由两个单独的类组成的 LinkedList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54065423/