从arraylist元素java

时间:2015-05-29 11:32:41

标签: java arraylist element

我有一个arraylist,它有一定数量的元素,每个元素包含两个值(天,分) company.getAnnouncements().get(0)

上面的代码将检索arraylist中的第一个元素,但我只想要该元素的美分组件。 你会怎么做?

public class Announcement implements Event {
private int day ;
private String statement;
    public Announcement(int day, String statement) {
            this.day = day;
            this.statement = statement;
    }

    @Override
    public String printEvent() {

        return statement;
    }

    public int getDay() {
        // TODO Auto-generated method stub
        return day;
    }


}

这是我所拥有的公告课程,而Dividend扩展了它

public class Dividend extends Announcement {

    private int cents;

        public Dividend(int day, int cents) {
            super(day,"");
            this.cents=cents;       

        }


        public double getCents() {
        return (double) (cents)/100;
        }

        public void setCents(int cents) {
            this.cents = cents;
        }


        @Override
        public String printEvent() {
            // TODO Auto-generated method stub
            return "Dividend of " + getCents() + " per share"  ;
        }

    }`

公司类

    public class Company implements Event {

private String name;
private String code;
private int currShares;
private double previousPrice;
private ArrayList <Dividend> dividends;
private ArrayList <Announcement> announcements;
private ArrayList <Trade> trades;

public Company(String code, String name, int currShares,
        double previousPrice) {
    this.name = name;
    this.code = code;
    this.setCurrShares(currShares);
    this.previousPrice = previousPrice;
    this.trades = new ArrayList <Trade> ();
     this.dividends = new ArrayList <Dividend> ();
     this.announcements = new ArrayList <Announcement> ();
}

public void addDividend(int day , int cents){
    dividends.add(new Dividend(day, cents));

 }

public void addAnnouncement(int day , String statement){
    announcements.add(new Announcement(day, statement));

}

public void addTrade(int day , int sharesTraded , double pricePerShare){
    trades.add(new Trade(day, sharesTraded, pricePerShare));

}


public String printEvent() {

    return "Printed!!";
}

public String getCode() {   
    return code;
}

public ArrayList<Dividend> getDividends() {
    return dividends;
}

public void setDividends(ArrayList<Dividend> dividends) {
    this.dividends = dividends;
}

public ArrayList<Announcement> getAnnouncements() {
    return announcements;
}

public void setAnnouncements(ArrayList<Announcement> announcements) {
    this.announcements = announcements;
}

public ArrayList<Trade> getTrades() {
    return trades;
}

public void setTrades(ArrayList<Trade> trades) {
    this.trades = trades;
}

public String getName() {
    return name;
}


public void addAnnouncement(int day, int cent) {
    // TODO Auto-generated method stub
    announcements.add(new Dividend(day,cent));
}

public int getCurrShares() {
    return currShares;
}

public void setCurrShares(int currShares) {
    this.currShares = currShares;
}


}

3 个答案:

答案 0 :(得分:4)

只需像访问任何其他getter一样访问它(假设AnnouncementgetCents方法):

int cents = company.getAnnouncements().get(0).getCents();

或者,如果你没有getCents方法(顺便说一下,你应该这样做):

int cents = company.getAnnouncements().get(0).cents;

我假设intcents的类型,您必须向我们提供Announcement想要确定的内容,以及您如何访问它。

<强>更新

编辑完成后,好像您不想要getAnnouncements而是getDividends。看起来有点像这样:

double cents = company.getDividends().get(0).getCents();

答案 1 :(得分:0)

我相信plot_surf会向您返回company.getAnnouncements(),而list会返回company.getAnnouncements().get(0)对象。

bean课程中必须有getCents()或其他内容,您的最终代码应如bean

或如果company.getAnnouncements().get(0).getCents()是bean类中的属性,您也可以cents

答案 2 :(得分:-1)

您需要知道值对象的类型才能知道如何访问它。

如果是另一个arraylist,你会这样做:

  

int cents = company.getAnnouncements()。get(0).get(1);