【问题标题】:How to set elements of a list who' valus are returning from two different lists?如何设置从两个不同列表返回值的列表元素?
【发布时间】:2021-08-01 05:23:04
【问题描述】:

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

如何完成这项任务。任何人都可以建议吗?

【问题讨论】:

  • 举个例子。文字描述很混乱。

标签: java arrays list data-structures


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 2019-10-31
    相关资源
    最近更新 更多