- 使用数组复制的时候,如果下面这样写,结果是list的所有元素值都是循环的最后一次附的值,为什么?
因为colvalue 只是地址,所有添加到ls的项都是这个地址值,所以大家指向的都是一个数组。可以使用数组克隆的方法,将数组复制,并且地址不同,这样就可以保存每次循环的值了。改后是这样的:String[] colvalue = { "processModelName", "initiateItemID", "createDate", "agent", "version", "description"}; ls.add(colvalue); for (int j = 0; j < size; j++) { colvalue[0] = pDef.getName(); colvalue[1] = item.getId(); colvalue[2] = new DateWrapper(item.getCreateDate()).toString(); colvalue[3] = "tewst"; colvalue[4] = pDef.getVersion().toString(); colvalue[5] = pDef.getDescription(); ls.add(colvalue); }
ls.add(colvalue.clone());
- ClassCaseException解决步骤。
java.lang.ClassCastException: java.lang.String
这个异常是说程序中对String类型进行强制类型转换的时候发生异常。
Before typecasting the object to String call
arlAccessLevel.get( 0 ).getClass().getName().
This will let you know why ClassCastException occured.
You can also check the type of object using
if(obj instanceof String) - 取得当前方法名
String currentMethodName = new Exception().getStackTrace()[0] .getMethodName();
java语言学习
最新推荐文章于 2022-04-13 18:00:43 发布