微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何设置值从两个不同列表返回的列表元素?

如何解决如何设置值从两个不同列表返回的列表元素?

我有一个列表 List accountInfo 和 AccountInfo 对象有多个值,其中一些值应该由从一个列表返回的值设置,比如 ListotherList1,而 AccountInfo 的其他一些值应该由从其他列表返回的值设置,比如 ListbotherList2。

如何完成这项任务。有人可以推荐吗?

解决方法

文字描述真的不是很清楚,但我会提供一些可能有帮助的代码:

package com.so.examples;

import java.util.ArrayList;
import java.util.List;

public class Example {

    private class AccountInfo
    {
        public int value1 = 0;
        public int value2 = 0;
        public int value3 = 0;
        public int value4 = 0;
        public AccountInfo(int v1,int v2,int v3,int v4)
        {
            value1 = v1;
            value2 = v2;
            value3 = v3;
            value4 = v4;
        }
    };
    void fillMainListWithSomeValuesFromAandSomeFromb(
            final List<AccountInfo> accountInfo,final List<AccountInfo> otherLista,final List<AccountInfo> otherListb)
    {
        for (int i = 0;i<10;++i) {
            final AccountInfo objectInOtherLista = otherLista.get(i);
            final AccountInfo objectInOtherListb = otherListb.get(i);
            // For each value decide whether it comes from a or b
            int v1 = objectInOtherLista.value1; // use otherLista
            int v2 = objectInOtherListb.value2; // use otherListb
            int v3 = objectInOtherListb.value3;
            int v4 = objectInOtherLista.value4;
            // Create the combined AccountInfo
            final AccountInfo someData = new AccountInfo(v1,v2,v3,v4);
            // and add it to the accountInfo list
            accountInfo.add(someData);
        }
    }
    void demo()
    {
        final List<AccountInfo> accountInfo = new ArrayList<>();
        final List<AccountInfo> otherLista = new ArrayList<>();
        final List<AccountInfo> otherListb = new ArrayList<>();
        for (int i = 0;i<10;++i) {
            final AccountInfo someDataa = new AccountInfo(i,i*i,i+3,2*i);
            final AccountInfo someDatab = new AccountInfo(i+7,3*i,5*i,3*i);
            otherLista.add(someDataa);
            otherListb.add(someDatab);
        }
        // Prepared some test data; now merge the lists
        fillMainListWithSomeValuesFromAandSomeFromb(
            accountInfo,otherLista,otherListb);        
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。