繁体   English   中英

Java中来自文件的数组数组

[英]Array of arrays from file in Java

我想从格式为的文件中读取数组

{{"text text text","text text","also text"},{"text text text","text text","also text"},{"text text text","text text","also text"}}

从文件中,不知道最好的方法。 我如何有效地分割行以将它们添加到for循环的ArrayList中?

string.split(",\\\\{")将返回由逗号分隔的行中的条目数组。 您可以使用此有些嵌套for s到填满你ArrayList秒。 尽管如果要保存到Android,则应使用SharedPreferences并保存项目集。

共享首选项示例:

    // Adds an item to the shared preferences list
    public void addItemToList(String item){
        SharedPreferences itemList = getSharedPreferences(PREFS_NAME, 0);
        Set<String> items =  itemList.getStringSet(filter, new HashSet<String>());
        Set<String> tItems =  new HashSet<String>();
        tItems.addAll(items); 

        tItems.add(item);

        SharedPreferences.Editor editor = itemList.edit();
        editor.putStringSet(filter, tItems);

        editor.commit();
    }

嵌套的示例:

String[] lineArray = line.split(",\\{");
for(int i =0; i < lineArray.length ; i++){
    String[] currentArray = lineArray[i].split(",");
    for(int j = 0; j < currentArray.length ; j++){
    ....

暂无
暂无

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

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