簡體   English   中英

如何合並具有不同變量類型的兩個列表

[英]How to merge two lists with different variable types

正如問題所述,我想知道將兩個具有不同變量類型的列表合並在一起的最佳方法是什么。 我不確定我解決這個問題的方法是否正確,所以我很樂意接受有關此問題的所有建議。 基本上我正在制作銀行應用程序。 當用戶完成時,一些交易數據被添加到表單(類型日期金額)的 txt 文件中。 我希望用戶稍后能夠進行更多交易,並將這些新交易添加到此 txt 文件的末尾。

我的問題是,當我重新閱讀用戶以前的交易時,我將它們作為字符串列表讀入

BufferedReader br = null;

    try {

        br = new BufferedReader(new FileReader(account.getName()+"Transactions.txt"));

        String line;
        while ((line = br.readLine()) != null) {
            transactions.add(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            br.close();
        }

但是,要添加到我的 txt 文件中的事務列表的類型是事務而不是字符串。

List<Transaction> transactionsToAdd = new ArrayList<Transaction>()

作為一個例子,這里有一些代碼在用戶存款的情況下。

    case 1:
            amount = scanner.DepositScanner();
            balance = account.Deposit(balance, amount);
            System.out.print("Your new balance is: " + balance);

            String depositFullDate = dateManager.getDateAsString();
            Transaction depositTransaction = new Transaction("Depsoit", amount, depositFullDate);
            transactionsToAdd.add(depositTransaction);

            break;

理想情況下,我希望代碼讀取用戶以前的交易,將他們的新交易添加到 txt 文件的末尾,然后在用戶完成后將所有這些保存到同一個 txt 文件中。 有沒有辦法以 Transaction 類型而不是 String 類型讀取用戶以前的交易,或者以某種方式將這兩個列表組合起來給我一個大列表以保存到我的 txt 文件中。

我的交易 class 如下

public class Transaction {
String details;
String date;

Double amount;



public String getDate() {
    return date;
}
public void setDate(String date) {
    this.date = date;
}
public Transaction(String details, double amount, String date) {
    super();
    this.details = details;
    this.date = date;
    
    this.amount = amount;
}
public String getDetails() {
    return details;
}
public void setDetails(String details) {
    this.details = details;
}
public Double getAmount() {
    return amount;
}
public void setAmount(double amount) {
    this.amount = amount;
}

}

編輯 1 - 為我的交易 class 添加了代碼

將您的交易列表轉換為 JSON 數組:(按照下面的鏈接)

將字符串列表轉換為 json 格式

然后,當您想創建一個新事務時,您可以將整個文件讀取為 JSON 數組,並從中獲取您的事務對象。 您將需要遍歷 JSON 數組來獲取您的事務對象。 (要獲取 Transaction 對象,您必須使用 ObjectMapper)。

看到這個:

https://stackoverflow.com/questions/23109531/json-array-to-pojo#:~:text=Cliente%5B%5D%20clientes%3D%20mapper.,JSON%20and%20in%20the%20POJO

我實際上遇到了同樣的問題,但提供給我的解決方案不涉及 JSON。 我也在使用掃描儀來讀取 .txt 文件而不是 BufferedReader(idk 如何使用它)。

我假設您的 txt 文件看起來像這樣。 僅包含這些單詞並由單個空格分隔。 因為你還沒有展示如何在 transactions.txt 中寫東西的方法

Deposit 42069 11/05/2021
Deposit 360 11/05/2021
//so on

這就是您讀取它並將其存儲在 object 類型的 ArrayList 中的方式。

//transactionToAdd is the name of your arraylist
//Transaction is the name of your class

String line = reader.readLine();
String[] array = line.split(" ")// use same delimiter used to write
if(array.lenght() ==3){ // to check if data has all three parameter 
    transactionToAdd.add(new Transaction(array[0], Double.parseDouble(array[1]), array[2] )); 
    // you have to handle case if Double.parseDouble(array[2]) throws exception
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM