简体   繁体   中英

Convert double list objects in object list to string

I am having a list of data objects. Each data object has a resultList of double elements which can also be null.

I want pull out this resultList of the data object and store them seperately in a string list:

List<List<String>> ar_List = new ArrayList<List<String>>();
for (int i = 0; i < dataList.size(); i++) {
    for (int j = 0; j < dataList.get(i).getResultList().size(); j++) {
        ar_List.add(dataList.get(i).getResultList().get(j).toString());
    }
}

However I still get an error at ar_List.add(dataList.get(i).getResultList().get(j).toString());

Any recommendations how to write the List Objects into ar_List ?

I appreciate your answers!

replace

ar_List.add(dataList.get(i).getResultList().get(j).toString());

with

ar_List.get(i).add(dataList.get(i).getResultList().get(j).toString());

Check if the data is null because calling toString() on a null object causes a NPE. And you need to get the List<String> and not List<List<String>>

if(dataList.get(i).getResultList().get(j) != null) {
       ar_List.get(i).add(dataList.get(i).getResultList().get(j).toString());
}

In order to check for null lists either filter the parent list for null objects or change the for condition.

for(in j = 0; j < dataList.get(i).getResultList().size(); dataList.get(i).getResultList().get(j)==null?j+=2:j++)

This jumps over the next row if the next row is null. But the better would be to clean the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM