将ArrayList的内容保存到外部存储

时间:2016-06-05 03:51:46

标签: java arraylist

我有一个使用

的平衡程序
import numpy as np

x=pd.DataFrame(df).T

rslt = pd.DataFrame(np.zeros((0,3)), columns=['top1','top2','top3'])
for i in x.columns:
    df1row = pd.DataFrame(x.nlargest(3, i).index.tolist(), index=['top1','top2','top3']).T
    rslt = pd.concat([rslt, df1row], axis=0)

print rslt

   top1  top2  top3
0  col7  col4  col9
0  col7  col5  col0
0  col7  col5  col0
0  col0  col7  col4
0  col0  col1  col2

我想知道如何使用

保存我添加到列表中的元素
ArrayList <Double> balance = new ArrayList();

所以下次打开程序时,会有相同的元素。

1 个答案:

答案 0 :(得分:0)

我建议使用序列化机制将对象存储到文件中。

声明您的类实现Serializable接口

class YourClass implements Serializable{
    private ArrayList<String> list = new ArrayList<String>();

    public void populateList(){
        list.add("xyz");
        list.add("abc");
    }
}

使用以下代码将列表序列化为文件:

FileOutputStream outputStream = new FileOutputStream("arrayList.list");
ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStream);
objOutputStream.writeObject(list);
objOutputStream.close();

从代码下面的文件使用中反序列化列表:

FileInputStream inputStream = new FileInputStream("arrayList.list");
ObjectInputStream objInputStream = new ObjectInputStream(inputStream);
List<String> list = (List<String>) objInputStream.readObject();
objInputStream.close();