制作SparseArray&lt;&gt;包含ArrayList <string> </string>

时间:2014-08-16 09:28:40

标签: java android arraylist sparse-array

我需要创建一个ArrayList列表,我的想法是:

 Map<Integer, ArrayList<String>> SentReportMap=new HashMap<Integer, ArrayList<String>>();

告诉:

 Use new SparseArray<ArrayList<String>>(...) instead for better performance

所以,我试试这个:

 SparseArray<String> SentReportMap = new SparseArray<String>();

 ArrayList<String> items=new ArrayList<String>();

 items.add(array.getProperty("To").toString());
 items.add(array.getProperty("Date").toString());

 SentReportMap.put(1, items);

但是要使用数据填充此数组,请返回此错误:

The method put(int, String) in the type SparseArray<String> is not applicable for the arguments (int, ArrayList<String>)

或者

The method put(int, String) in the type SparseArray<String> is not applicable for the arguments (ArrayList<String>, int)

哪里错了,什么是制作阵列的arraylist的更好的解决方案!?

2 个答案:

答案 0 :(得分:0)

更改

 SparseArray<String> SentReportMap = new SparseArray<String>();

SparseArray<ArrayList<String>> SentReportMap = new SparseArray<ArrayList<String>>();

SparseArray是更好的解决方案,因为:

SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Objects, both because it avoids auto-boxing keys and its data structure doesn't rely on an extra entry object for each mapping.

查看此链接以获取更多信息。

http://developer.android.com/reference/android/util/SparseArray.html

答案 1 :(得分:0)

SentReportMap的声明是错误的,它应该是:

SparseArray<ArrayList<String>> SentReportMap = new SparseArray<ArrayList<String>>();