-3

I am having issue to store all values of ArrayList<ArrayList<String>> into ArrayList<String>. Here stylistIDArray and durationArray are array of array. I want to store all their values in stylistId and duration respectively. The stylistid and duration are array of string.

Here's my attempt, but it stores only the last item of each array of array.

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(int i=0; i<stylistIDArray.size(); i++) {
    stylistId = stylistIDArray.get(i);
    duration = durationArray.get(i);
}

Note : I have already tried this, but doesn't work for me.

8
  • 2
    Possible duplicate of How to add one arraylist to other arraylist in android? Commented Jun 15, 2018 at 11:32
  • its not working Commented Jun 15, 2018 at 11:55
  • i tried with the code in that link Commented Jun 15, 2018 at 11:56
  • In that case you should add that link in to your question.. and mentioned that answer is not working for you Commented Jun 15, 2018 at 11:56
  • i tried with the link which you had given its not working for me stackoverflow.com/questions/8666678/… Commented Jun 15, 2018 at 12:01

2 Answers 2

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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!

4 Comments

If i follow this it asking me to change stylistid array into arrayofarray list
i want to store it in arraylist not in arrayofarray list
of-course it will store in arraylist you have to typecast.
k i will try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.