java - arrayList 存储的不确定性

标签 java arrays database arraylist

public void openFile(){
        try {
            l = new Formatter("try.txt");
            System.out.println("Opened File");
        } catch (Exception e){
            System.out.println("Did not open file");
        }
    }
    public void addRecord(){
        l.format("%s", nameField.getText());
        System.out.println("Added Name" + " " + nameField.getText());
        }
    public void onClose(){
        l.close();
    }

/*

So I am trying to store user data, I have a gui and anytime they click sign up i want to store the namefield in an array list, is it possible that anytime I close and reopen the program and someone else puts in their name it just adds to the array list instead of overriding what the earlier person put in?
Example is in my gui i type in mike as name, I close it and reopen it and put in john as the name, would that add mike and john or would it be just john? and how do I make them co-exist?
Thank you

它会将其读取到文件中,但每次我关闭程序并再次运行它时,它都会替换第一行上的内容。

最佳答案

List (其中 ArrayList 是一种实现)您可以只使用 add方法。例如,如果您有一个包含单个对象(字符串“Mike”)的列表,然后执行 namesList.add("John") (其中 namesListList<String> )那么列表将包含两个元素:“John”和“Mike”。

但是,您还提到了在程序执行之间保留数据。默认情况下,所有列表都纯粹内存数据存储,并且每次关闭并重新打开程序时都会被清除。为了存储它们,您需要某种形式的数据持久性。在重型方面,这将是一个完整的数据库,但对于具有简单需求的小型项目,您可以使用 SQLite - 甚至只是一个文本文件。

关于java - arrayList 存储的不确定性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758642/

相关文章:

java - 在 AsyncTask 中添加 View ( TextView )

java - RabbitMQ 同一队列的多个消费者

arrays - php 5.4 中的数组错误

database - 由于数据库表约束,TClientDataset ApplyUpdates 错误

sql-server - 将表主 ID 连接为具有总和值的字符串

java - 如何消除按键延迟?

java - 将 Linq4j 与内存中基元 List 和 Map 结合使用

python - 如何根据第一个数组的元素从另一个数组中减去一个 NumPy 数组

arrays - 创建具有固定计数的 Swift Array<Float> 的最快方法

php - 添加文章到 Jooma 时出现 500 内部服务器错误! 3.5 数据库