How to store all ArrayList> values into ArrayList?

前端 未结 2 1533
借酒劲吻你
借酒劲吻你 2021-01-29 14:51

I am having issue to store all values of ArrayList> into ArrayList. Here stylistIDArray and

2条回答
  •  滥情空心
    2021-01-29 15:35

    To be generic, First let be an list of list of objects

    List> listOfList;
    

    That you want to put into a list of object

    List result;
    
    
    

    Note the result list will contain every object contain is the input list. This transformation will loose the information of which object was in which list.

    You have to loop through the listOfList. At each loop you obtain a list of object (List listOfObject). Then loop through these lists to obtain every object (Object o). Then add these object to the result list (result.add(o)).

    for(List listOfObject : listOfList) {
        for(Object o : listOfObject) {
            result.add(o);
        }
    }
    
    
    

    In your case, the problem is that you use affectation instead of add(). At every loop this replaces the value by the new one. So at the end you have stored only the last item of each list.

    stylistId=stylistIDArray.get(i); //This replace the existing stylistId
    

    Instead try something like

    ArrayList> stylistIDArray;
    ArrayList> durationArray;
    stylistIDArray = differentGenderServicesAdapter.getSelectedStylistIdArray();
    durationArray = differentGenderServicesAdapter.getSelectedDurArray();
    
    ArrayList stylistId = new ArrayList<>();
    ArrayList duration = new ArrayList<>();
    
    for(ArrayList l : stylistIDArray) {
        for(String s : l) {
            stylistId.add(s);
        }
    }
    for(ArrayList l : durationArray ) {
        for(String s : l) {
            duration.add(s);
        }
    }
    

    提交回复
    热议问题