保存 ArrayList 内容的最佳方法是什么? [英] What is the best way to save the contents of an ArrayList?

查看:29
本文介绍了保存 ArrayList 内容的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存一个 ArrayList 以便它是持久的.内容可以更改.在 android 中解决这个问题的最佳方法是什么?

解决方案

您的问题类似于问我有这袋东西——我应该将这些东西转移到盒子、桶、桶还是垃圾箱?".

在不知道东西"是什么的情况下,我们无法轻易回答这个问题.

您的一般选择是使用数据库(也许实际上首先摆脱了 ArrayList)或持久化到文件(序列化、XML、JSON 等).

一般来说,SQLite 数据库具有优势,即使用事务并且通常比仅编写自己的文件更健壮.但是,您的 ArrayList 可能存储的某些内容在数据库中可能无法正常工作.当然,您的 ArrayList 可能会存储一些甚至根本无法持久化的内容(例如,套接字、小部件).

I want to save an ArrayList so that it is persistent. The contents can change. What is the best way of approaching this in 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.).

Generally speaking, a SQLite database has advantages, in that is uses transactions and will be generally more robust than just writing your own file. However, there are things your ArrayList might store that might not work well in a database. Of course, there are things your ArrayList might store that might not even be able to be persisted at all (e.g., a socket, a widget).

这篇关于保存 ArrayList 内容的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆