【发布时间】:2014-08-12 11:53:54
【问题描述】:
我有这个方法可以从一个文本文件中获取一个数组列表:
private ArrayList<String[]> tempList(){
String temp = null;
ArrayList<String[]> tempList = new ArrayList<String[]>();
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(getAssets().open(getFilename())));
while ((temp = bReader.readLine()) !=null){
String[] value = temp.split(",");
tempList.add(value);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tempList;
}
该文件只有用逗号分隔的字母,没有空格或空行。问题是列表的第一个字母前面有一个空格。其他字母都没问题。
这是包含本地字母表的 txt 文件:
Α,Β,Γ
Δ,Ε,Ζ
Η,Θ,Ι
Κ,Λ,Μ
Ν,Ξ,Ο
Π,Ρ,Σ
Τ,Υ,Φ
Χ,Ψ,Ω
知道有什么问题吗?
【问题讨论】:
-
我已经尝试过你的代码并且运行良好,你调试过代码然后你就知道 templList 的更新值了。
-
如果字符串中有任何前导或尾随空格,请使用 String 类的 trim() 函数..