被for循環坑了

測試代碼以下app

@Test
    public void test_2(){
        List<AuthorizeContractBean> list = new ArrayList<>();
        AuthorizeContractBean b = new AuthorizeContractBean();
        for (int i = 0; i < 1000; i++) {
            b.setBorrowerId(String.valueOf(i));
            if(i % 2 == 0){
                b.setPlatformNo("aaaa");
                b.setFileUrl("aaa-url");
            } else {
                b.setPlatformNo("bbbb");
                b.setFileUrl("bbb-url");
            }
            list.add(b);
            //logger.info(b.toString());
        }
        for (int i = 0; i < list.size(); i++) {
            logger.info(list.toString());
        }
    }

輸出日誌以下測試

16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.420 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.421 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.421 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.421 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.421 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.421 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}
16:07:59.421 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}

驚呆,list裏裝的id都變成了999,也就是都變成了最後一個add進list的對象。url

當時在封裝一個帶list對象的bean對象,發送給接口,再處理返回結果。返回結果發現幾十個id都同樣,鬱悶了,覺得把for循環裏的i寫成1了,仔細排查代碼才發現。日誌

總之,不要在for循環裏改變在for循環外建立的對象,這樣會出現意想不到的後果!code

修改後代碼以下:orm

@Test
    public void test_2(){
        List<AuthorizeContractBean> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            // 把對象的建立放進for循環裏
            AuthorizeContractBean b = new AuthorizeContractBean();
            b.setBorrowerId(String.valueOf(i));
            if(i % 2 == 0){
                b.setPlatformNo("aaaa");
                b.setFileUrl("aaa-url");
            } else {
                b.setPlatformNo("bbbb");
                b.setFileUrl("bbb-url");
            }
            list.add(b);
            //logger.info(b.toString());
        }
        for (int i = 0; i < list.size(); i++) {
            logger.info(list.get(i).toString());
        }
    }

此時運行測試類,輸出結果以下:對象

16:11:49.029 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='0', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='1', fileUrl='bbb-url', platformNo='bbbb'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='2', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='3', fileUrl='bbb-url', platformNo='bbbb'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='4', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='5', fileUrl='bbb-url', platformNo='bbbb'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='6', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='7', fileUrl='bbb-url', platformNo='bbbb'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='8', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='9', fileUrl='bbb-url', platformNo='bbbb'}
16:11:49.031 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='10', 
fileUrl='aaa-url', platformNo='aaaa'}

······
16:11:49.073 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='992', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.073 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='993', fileUrl='bbb-url', platformNo='bbbb'}
16:11:49.073 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='994', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.073 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='995', fileUrl='bbb-url', platformNo='bbbb'}
16:11:49.073 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='996', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.073 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='997', fileUrl='bbb-url', platformNo='bbbb'}
16:11:49.073 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='998', fileUrl='aaa-url', platformNo='aaaa'}
16:11:49.073 [main] INFO  c.i.ups.app.contract.Test.ThreadTest - AuthorizeContractBean{borrowerId='999', fileUrl='bbb-url', platformNo='bbbb'}

這樣就能夠了!輸出id爲i的值從0到999接口

相關文章
相關標籤/搜索