用toArray()将集合转为数组时,转换成的是Object类型的数组。
如果数据类型不满足自己要求,不能在转换时强转,应该在赋值的时候再强转成自己需要的类型。
另外复习下valueOf方法
Integer.valueOf(“1005”) 其实等价于 new Integer(“1005”)。
valueOf可以转换字符,double,short,等等类型。同时 Double Long等对象也有相应的valueOf方法。
正确做法:
List<String> schoolNameList = studentsRepository.findBySchoolNameIsNotNull1();
List<String> schoolList = studentsRepository.findBySchoolNameIsNotNull2();
Object[] schoolNameArray = schoolNameList.toArray();
Object[] schoolArray = schoolList.toArray();
int length = schoolNameArray.length;
List<TreeDTO> treeDTOList = new ArrayList<>();
for (int i = 0; i < length; i++) {
TreeDTO treeDTO = new TreeDTO();
treeDTO.setName((String) schoolNameArray[i]);
treeDTO.setId(Long.valueOf((String) schoolArray[i]));
treeDTOList.add(treeDTO);
}