如何存储所有ArrayList< ArrayList< String>>值放入ArrayList< String>中? [英] How to store all ArrayList<ArrayList<String>> values into ArrayList<String>?

查看:60
本文介绍了如何存储所有ArrayList< ArrayList< String>>值放入ArrayList< String>中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 ArrayList< ArrayList< String>> 的所有值存储到 ArrayList< String> 中时遇到问题.这里的 stylistIDArray durationArray 是数组的数组.我想将所有它们的值分别存储在 stylistId duration 中. stylistid duration 是字符串数组.

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.

推荐答案

一般来说,首先让其成为对象列表

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

List<List<Object>> listOfList;

您要放入对象列表的

List<Object> result;

注意,结果列表将包含每个对象,包含的对象是输入列表.这种转换将丢失哪个对象位于哪个列表中的信息.

您必须遍历 listOfList .在每个循环中,您将获得对象列表( List< Object> listOfObject ).然后遍历这些列表以获取每个对象( Object o ).然后将这些对象添加到结果列表( result.add(o)).

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);
    }
}


在您的情况下,问题在于您使用情感而不是 add().在每个循环中,它将用新值替换该值.因此,最后您只存储了每个列表的最后一项.


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

请尝试类似

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);
    }
}

这篇关于如何存储所有ArrayList&lt; ArrayList&lt; String&gt;&gt;值放入ArrayList&lt; String&gt;中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆