简体   繁体   English

保存ArrayList内容的最佳方法是什么?

[英]What is the best way to save the contents of an ArrayList?

I want to save an ArrayList so that it is persistent. 我想保存一个ArrayList,以便它是持久的。 The contents can change. 内容可以改变。 What is the best way of approaching this in android? 在android中接近这个的最佳方法是什么?

Your question is akin to asking "I have this bag of stuff -- should I transfer the stuff to a box, bucket, barrel, or bin?". 你的问题类似于问“我有这个东西 - 我应该将东西转移到盒子,桶,桶还是垃圾桶?”。

Without knowing what the "stuff" is, we cannot easily answer the question. 如果不知道“东西”是什么,我们就不能轻易回答这个问题。

Your general options are either to use a database (perhaps actually getting rid of the ArrayList in the first place) or persistence to a file (serialization, XML, JSON, etc.). 您的一般选项是使用数据库(可能实际上首先摆脱了ArrayList )或持久化到文件(序列化,XML,JSON等)。

Generally speaking, a SQLite database has advantages, in that is uses transactions and will be generally more robust than just writing your own file. 一般来说,SQLite数据库具有优势,因为它使用事务并且通常比仅编写自己的文件更健壮。 However, there are things your ArrayList might store that might not work well in a database. 但是,您的ArrayList可能存储的内容在数据库中可能无法正常工作。 Of course, there are things your ArrayList might store that might not even be able to be persisted at all (eg, a socket, a widget). 当然,您的ArrayList可能存储的内容甚至可能根本不能ArrayList (例如,套接字,小部件)。

I am not familiar with Android SDK, but I am with Java. 我不熟悉Android SDK,但我使用的是Java。 One thing you can easily do is serialize the ArrayList. 您可以轻松完成的一件事是序列化ArrayList。 Then when you read it again, it'll be an ArrayList object which you can readily use as if you had just created it. 然后,当您再次阅读它时,它将是一个ArrayList对象,您可以轻松使用它,就像您刚刚创建它一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM