如何在TreeSet或TreeMap中添加ArrayList元素(How to add ArrayList elements in TreeSet or TreeMap)
众所周知,我们需要实现Comparable接口,并在使用TreeSet时添加compareTo()。 不这样做会抛出ClassCastException。 现在我有一个TreeSet,我需要添加ArrayLists作为TreeSet的元素。
如果我们写:
ArrayList al = new ArrayList(); ArrayList al2 = new ArrayList(); ArrayList al3 = new ArrayList(); TreeSet ts = new TreeSet(); ts.add(al); ts.add(al2); ts.add(al3);
它抛出ClassCastException。
问题 :如何将ArrayList istances(不是它们的元素)添加到TreeSet或TreeMap?
As we all know, we need to implement Comparable interface and add compareTo() when we work with TreeSet. Not doing so will throw ClassCastException. Now I have a TreeSet and I need to add ArrayLists as elements of TreeSet.
if we write:
ArrayList al = new ArrayList(); ArrayList al2 = new ArrayList(); ArrayList al3 = new ArrayList(); TreeSet ts = new TreeSet(); ts.add(al); ts.add(al2); ts.add(al3);
It throws ClassCastException.
Question: How can I add ArrayList istances (not their elements) to a TreeSet or TreeMap?
原文:https://stackoverflow.com/questions/35613475
最满意答案
我会从另一个答案重复一遍:
从API级别8(Android 2.2)开始,如果将应用程序构建为可调试的,可以使用shell
run-as
命令将命令或可执行文件作为特定用户/应用程序运行,或者切换到应用程序的UID
,以便您可以访问其数据目录。因此,如果您希望从设备中拉出应用程序数据库,则应运行应用程序的调试版本,连接到
adb shell
并运行以下命令:run-as com.yourpackage sh -c "cat ~/databases/db-file" > /sdcard/db-file.sqlite
这将将您的
db-file
复制到SD卡/外部存储的根目录。 现在,您可以使用文件管理器,adb pull
或其他任何您喜欢的方式轻松获取。 请注意,通过这种方法,您的应用程序不需要具有WRITE_EXTERNAL_STORAGE
权限,因为复制由可以永久写入外部存储的shell用户完成。在Linux / Mac系统上,可以使用以下命令将数据库直接复制到您的计算机,而无需输入adb shell:
adb shell 'run-as com.yourpackage sh -c "cat ~/databases/db-file"' > db-file.sqlite
然而,由于CR / LF符号转换,Windows将无法正常工作。 在那里使用以前的方法。
I'll repeat myself from another answer:
Starting from API level 8 (Android 2.2), if you build the application as debuggable, you can use the shell
run-as
command to run a command or executable as a specific user/application or just switch to theUID
of your application so you can access its data directory.So if you wish to pull your application database from the device you should run the debug build of the application, connect with
adb shell
and run the following command:run-as com.yourpackage sh -c "cat ~/databases/db-file" > /sdcard/db-file.sqlite
This will copy your
db-file
to the root of your SD card / external storage. Now you can easily get it from there by using file manager,adb pull
or whatever else you like. Note that with this approach, there is NO need for your app to haveWRITE_EXTERNAL_STORAGE
permission, as the copying is done by the shell user who can always write to the external storage.On Linux/Mac systems there is a possibility to copy a database directly to your computer with the following command one can use without entering the adb shell:
adb shell 'run-as com.yourpackage sh -c "cat ~/databases/db-file"' > db-file.sqlite
This however will not work correctly on Windows because of CR/LF symbols conversion. Use the former method there.
相关问答
更多-
试试这个,让我知道它是否在那里创建了文件 public class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, context.getExternalFilesDir("MyDatabase") + "/" + DATABASE_NAME, null, DATABASE_VERSION); } 或者如果想要使用内 ...
-
有一个命令行版本的sqlite3可用,它允许您直接访问/查看/修改Android设备上的adb(root)shell中的SQLite数据库,而无需将数据库复制到您的计算机。 当然,你需要设备上的root权限,否则你无法访问内部数据库的应用专用目录。 编辑:一些Android SQLite工具似乎实现了类似远程接口或GUI的命令行sqlite3工具,如上所述。 我没有测试过它,但是SQLiteStudio和它的DBAndroid插件结合起来似乎可以提供从PC到Android手机上数据库的GUI访问(无需复制数 ...
-
我会从另一个答案重复一遍: 从API级别8(Android 2.2)开始,如果将应用程序构建为可调试的,可以使用shell run-as命令将命令或可执行文件作为特定用户/应用程序运行,或者切换到应用程序的UID ,以便您可以访问其数据目录。 因此,如果您希望从设备中拉出应用程序数据库,则应运行应用程序的调试版本,连接到adb shell并运行以下命令: run-as com.yourpackage sh -c "cat ~/databases/db-file" > /sdcard/db-file.sqli ...
-
实现您所期望的一种常见方式是使用ADB pull命令。 在大多数情况下,我更喜欢的另一种方法是将数据库通过代码复制到SD卡: try { File sd = Environment.getExternalStorageDirectory(); if (sd.canWrite()) { String currentDBPath = "/data/data/" + getPackageName() + "/databases/yourdatabasename"; ...
-
您无法从生产硬件上的DDMS访问内部存储上的文件。 您可以尝试使用adb shell run-as来使用命令行adb客户端提取数据库: https : //stackoverflow.com/a/8434477/115145 adb shell run-as com.corp.appName cat files/myFile.txt > /mnt/sdcard/myFile.txt You cannot access files on internal storage from DDMS on produ ...
-
如何在Android设备上升级\转换Sqlite数据库(How to upgrade\convert a Sqlite database on an Android device)[2021-09-09]
似乎问题是WAL模式已打开。 http://www.sqlite.org/wal.html WAL应该默认为Off(在我所有的Nexus设备上)。 我只能假设一些制造商认为将设置默认为On会很有趣,因此使数据库与早期版本不兼容。 在3.7.0之前的任何版本的SQLite中都不支持Wal模式,如果尝试,则如果在其上设置了此标志,则无法打开数据库。 It seems the problem is that WAL mode was turned on. http://www.sqlite.org/wal.htm ... -
如果你想查看数据库数据或方案,你可以尝试使用Stetho工具。 If you want to look database data or schemes, you can try to use Stetho tool.
-
试试这个代码 在Doc Directory中创建数据库... -(void)createdatabase { NSString *databaseName = @"Card.sqlite"; // Name Of DataBase. NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *do ...
-
你可以使用Root Explore,在此之前,你必须root你的设备和用户root权限才能运行应用程序,然后打开并找到数据文件夹并打开文件夹然后继续查找数据文件夹并打开,你会在这个设备中找到app文件,找到你的包名,你可以找到db,你可以使用db复制到sdcard you can user Root Explore,before this,you must root your device and user root permission to run the app ,then open and find ...
-
所以我发现这是有效的: adb root shell adb shell 然后我就可以导航到我的应用程序的数据库文件夹并打开它。 So i find this to be working: adb root shell adb shell Then I'm able to navigate to my app's database folder and open it.