我想创建一个常量ArrayList,但我保持返回空结果。
请注释这是声明静态最终数组列表的正确方法吗?
public final class Constants {
public static ArrayList<String[]> points = new ArrayList<String[]>();
{
points.add( new String[] {"A","5.42420782","100.2680779" } );
points.add( new String[] {"B","5.42253496","100.26634007" } );
points.add( new String[] {"C","5.4196543","100.2645124" } );
points.add( new String[] {"D", "5.41808928","100.26244618" } );
}
public static final List<ArrayList<String[]>> fix_points = Collections.unmodifiableList(Arrays.asList(points));
}
尝试以循环方式返回。
for(ArrayList<String[]> item : Constants.fix_points) {
System.out.println(item);
}
打印显示空[]。
答案 0 :(得分:4)
您需要了解启动块和静态块之间的区别。
{
points.add( new String[] {"A","5.42420782","100.2680779" } );
points.add( new String[] {"B","5.42253496","100.26634007" } );
points.add( new String[] {"C","5.4196543","100.2645124" } );
points.add( new String[] {"D", "5.41808928","100.26244618" } );
}
在创建常量实例时调用。
哪里
static {
points.add( new String[] {"A","5.42420782","100.2680779" } );
points.add( new String[] {"B","5.42253496","100.26634007" } );
points.add( new String[] {"C","5.4196543","100.2645124" } );
points.add( new String[] {"D", "5.41808928","100.26244618" } );
}
在类加载时调用。
您需要后一个块,即静态块。
答案 1 :(得分:1)
替换下面的行
List<ArrayList<String[]>> fix_points = Collections.unmodifiableList(Arrays.asList(points));
final List<String[]> fix_points = Collections.unmodifiableList(points);
final List<String[]> fix_points = Collections.unmodifiableList(points);
for(String[] item : fix_points) {
for(int i=0;i<item.length;i++)
System.out.println(item[i]);
}
答案 2 :(得分:0)
我不知道为什么你需要一个List<ArrayList<String[]>>
,但产生的输出应该是(String[].toString()
)
[[Ljava.lang.String;@29ce4d9c, [Ljava.lang.String;@b24c3a5, [Ljava.lang.String;@28e1c37a, [Ljava.lang.String;@1c0b972b]
您可以使用
打印确切的内容for(ArrayList<String[]> item : Constants.fix_points) {
for (String[] i : item) {
Log.d(getClass().getSimpleName(), " " + i[0]+ " " + i [1]);
}
}