不同list 的更改数据的问题

list1:[{ ToStoreId=101, barcode=999878457}
,{ ToStoreId=101, barcode=999878248}]
list2:[{storeidName=南京, ToStoreId=101},
{storeidName=无锡, ToStoreId=102}] 。
上面这个两个list,当ToStoreId相同时,怎么把list中的ToStoreId替换成list2中的storeidName
啊。

3个回答

这种建议你去参考一下冒泡排序

遍历取出来比较呗,另外这不是数组么?

package com.kevindai.json;

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

/**
 * Created by daiwenkai on 2017/4/24.
 */
public class ListTest {
    static class AddressTest{
        private String ToStoreId;
        private String barcode;
        private String storeidName;

        public String getStoreidName() {
            return storeidName;
        }

        public void setStoreidName(String storeidName) {
            this.storeidName = storeidName;
        }

        public String getToStoreId() {
            return ToStoreId;
        }

        public void setToStoreId(String toStoreId) {
            ToStoreId = toStoreId;
        }

        public String getBarcode() {
            return barcode;
        }

        public void setBarcode(String barcode) {
            this.barcode = barcode;
        }
    }

    public static void main(String[] args) {
        AddressTest add1 = new AddressTest();
        add1.setToStoreId("101");
        add1.setBarcode("999878457");
        AddressTest add2 = new AddressTest();
        add2.setToStoreId("102");
        add2.setBarcode("999878248");
        List<AddressTest> list1 = new ArrayList<>();
        list1.add(add1);
        list1.add(add2);

        AddressTest add3 = new AddressTest();
        add3.setToStoreId("101");
        add3.setStoreidName("南京");
        AddressTest add4 = new AddressTest();
        add4.setToStoreId("102");
        add4.setStoreidName("无锡");
        List<AddressTest> list2 = new ArrayList<>();
        list2.add(add3);
        list2.add(add4);


        Iterator<AddressTest> iterator = list1.iterator();
        while (iterator.hasNext()){
            AddressTest test = iterator.next();
            for (int i = 0; i < list2.size(); i++) {
                if(test.getToStoreId().equals(list2.get(i).getToStoreId())){
                    test.setToStoreId(list2.get(i).getStoreidName());
                }
            }
        }


        for (AddressTest addressTest : list1) {
            System.out.println(addressTest.getToStoreId());
            System.out.println(addressTest.getBarcode());
        }
    }
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!