无法在多个对象上设置值

时间:2017-04-18 22:45:29

标签: java

我创建了一个名为annuals的对象...我有这个对象的getter和setter,它实现了可比性。当我创建此对象的多个以添加到列表时,我最终得到一个具有值的对象,但所有后续对象都具有空值。我该如何防止这种情况?代码被对象和创建对象分解。

提前致谢...

对象:

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CreateCompanies
{
    private List<CompanyInfoDO> companiesList = new ArrayList<CompanyInfoDO>(); 
    /**
     * @return the companiesList
     */
    public List<CompanyInfoDO> getCompaniesList()
    {
        return companiesList;
    }

    /**
     * @param companiesList the companiesList to set
     */
    public void setCompaniesList(List<CompanyInfoDO> companiesList)
    {
        this.companiesList = companiesList;
    }

    public CreateCompanies()
    {
        CompanyInfoDO company = new CompanyInfoDO();
        company.setCompanyName("Ford");
        company.setDate(Calendar.getInstance().getTime().toString());
        company.setShares("39000000000");
        company.setTicker("f");
        company.setStockPrice(BigDecimal.valueOf(11.14));
        company.setTotalOutstandingSharesAnnuals(getTotalSharesOutstandingList());
        company.setNetIncomeAnnuals(getNetIncomeList());
        companiesList.add(company);

    }

    private List<Annuals> getTotalSharesOutstandingList()
    {
        List<Annuals> annualsList = new ArrayList<Annuals>();
        Annuals annual = new Annuals();
        annual.setDataYear(BigDecimal.valueOf(2013));
        annual.setValue(BigDecimal.valueOf(3984.0));
        annual.setType(InvestConstants.TOTAL_SHARES_OUTSTANDING);

        Annuals annual1 = new Annuals();
        annual.setDataYear(BigDecimal.valueOf(2014));
        annual.setValue(BigDecimal.valueOf(4009.0));
        annual.setType(InvestConstants.TOTAL_SHARES_OUTSTANDING);

        Annuals annual2 = new Annuals();
        annual.setDataYear(BigDecimal.valueOf(2015));
        annual.setValue(BigDecimal.valueOf(4031.0));
        annual.setType(InvestConstants.TOTAL_SHARES_OUTSTANDING);

        Annuals annual3 = new Annuals();
        annual.setDataYear(BigDecimal.valueOf(2016));
        annual.setValue(BigDecimal.valueOf(3931.0));
        annual.setType(InvestConstants.TOTAL_SHARES_OUTSTANDING);

        annualsList.add(annual);
        annualsList.add(annual1);
        annualsList.add(annual2);
        annualsList.add(annual3);
        return annualsList;
    }

    private List<Annuals> getNetIncomeList()
    {
        List<Annuals> annualsList = new ArrayList<Annuals>();
        Annuals annual = new Annuals();
        annual.setDateCalculated(Calendar.getInstance().getTime());
        annual.setType(InvestConstants.NET_INCOME);
        annual.setValue(BigDecimal.valueOf(11953.0));
        annual.setDataYear(BigDecimal.valueOf(2013));
        System.out.println("Annual: "+ annual.toString());
        annualsList.add(annual);

        Annuals annual2 = new Annuals();
        annual.setDateCalculated(Calendar.getInstance().getTime());
        annual.setType(InvestConstants.NET_INCOME);
        annual.setValue(new BigDecimal(1231.0));
        BigDecimal val2 = new BigDecimal(2014);
        annual.setDataYear(val2);
        System.out.println("Annual 2 : "+ annual2.toString());

        Annuals annual3 = new Annuals();
        annual.setDateCalculated(Calendar.getInstance().getTime());
        annual.setType(InvestConstants.NET_INCOME);
        annual.setValue(BigDecimal.valueOf(7373.0));
        annual.setDataYear(BigDecimal.valueOf(2015));

        Annuals annual4 = new Annuals();
        annual.setDateCalculated(Calendar.getInstance().getTime());
        annual.setType(InvestConstants.NET_INCOME);
        annual.setValue(BigDecimal.valueOf(4596.0));
        annual.setDataYear(BigDecimal.valueOf(2016));

        //annualsList.add(annual);
        annualsList.add(annual2);
        annualsList.add(annual3);
        annualsList.add(annual4);
        System.out.println("Net Annuals List: Creating Company: "+annualsList.toString());
        return annualsList;
    }

创建对象:

date_time1

2 个答案:

答案 0 :(得分:4)

Annuals annual1 = new Annuals();
annual.setDataYear(BigDecimal.valueOf(2014));
annual.setValue(BigDecimal.valueOf(4009.0));
annual.setType(InvestConstants.TOTAL_SHARES_OUTSTANDING);

您正在创建名为annual1annual2等的新实例,但只能修改annual

答案 1 :(得分:2)

您总是重复使用相同的变量annual

在创建Annual的新实例并将实例分配给变量annual1annual2之后,请注意您的代码,重新使用旧变量annual

Annuals annual1 = new Annuals();

annual.setDataYear(BigDecimal.valueOf(2014));