How to store all ArrayList> values into ArrayList?

前端 未结 2 1498
借酒劲吻你
借酒劲吻你 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<List<Object>> listOfList;
    

    That you want to put into a list of object

    List<Object> 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<Object> 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<Object> 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<ArrayList<String>> stylistIDArray;
    ArrayList<ArrayList<String>> durationArray;
    stylistIDArray = differentGenderServicesAdapter.getSelectedStylistIdArray();
    durationArray = differentGenderServicesAdapter.getSelectedDurArray();
    
    ArrayList<String> stylistId = new ArrayList<>();
    ArrayList<String> duration = new ArrayList<>();
    
    for(ArrayList<String> l : stylistIDArray) {
        for(String s : l) {
            stylistId.add(s);
        }
    }
    for(ArrayList<String> l : durationArray ) {
        for(String s : l) {
            duration.add(s);
        }
    }
    
    0 讨论(0)
  • 2021-01-29 15:49

    You doing wrong operation with arraylist, in loop you are getting data from stylistIDArray and assign to aryalist, not inserting in list, have look this

    stylistIDArray=differentGenderServicesAdapter.getSelectedStylistIdArray();
                            durationArray=differentGenderServicesAdapter.getSelectedDurArray();
    
                            ArrayList<String>stylistId=new ArrayList<>();
                            ArrayList<String>duration=new ArrayList<>();
    
                           for(int i=0; i<stylistIDArray.size(); i++) {
                               stylistId.add(stylistIDArray.get(i));
                               duration.add(durationArray.get(i));
                           }
    

    Hope it will help you!

    0 讨论(0)
提交回复
热议问题