Zac*_*ell 3 java android arraylist
目前我有这个代码:
ArrayList<String> items = new ArrayList<String>();
AssetManager assetManager = getApplicationContext().getAssets();
try {
items.addAll(Arrays.asList(assetManager.list("")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这给了我资产文件夹中所有文件名的arraylist.但是我需要对此进行过滤,以便arraylist只有具有.txt扩展名的文件的文件名,然后从每个项目名称中删除.txt.
所以当前的代码会导致:
test.txt
pi.txt
sounds
hippo.png
square.xml
seven.txt
Run Code Online (Sandbox Code Playgroud)
但当我需要时,arraylist的内容将是:
test
pi
seven
Run Code Online (Sandbox Code Playgroud)
所以你需要这样做
ArrayList<String> items = new ArrayList<String>();
AssetManager assetManager = getApplicationContext().getAssets();
for (String file : assetManager.list("")) {
if (file.endsWith(".txt"))
items.add(file);
}
Run Code Online (Sandbox Code Playgroud)
如果.txt要从文件名中删除扩展名,可以执行此操作
...
items.add(file.replaceAll(".txt$", ""));
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1443 次 |
| 最近记录: |