从 arraylist 元素中获取特定值 java
get a specific value from arraylist element java
我有一个数组列表,其中包含一定数量的元素,每个元素包含两个值(天,美分)
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;
}
}
这是我的公告class,股息延长
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" ;
}
}`
和公司Class
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;
}
}
我相信 company.getAnnouncements()
returns 你是 list
,然后 company.getAnnouncements().get(0)
你会到达 bean
对象。
bean
class 中必须有 getCents()
或其他内容,您的最终代码应类似于 company.getAnnouncements().get(0).getCents()
或者如果 cents
是 bean class 中的一个属性,您也可以 company.getAnnouncements().get(0).cent
就像访问任何其他 getter 一样访问它(这是假设,Announcement
有一个 getCents
方法):
int cents = company.getAnnouncements().get(0).getCents();
或者,如果您没有 getCents
方法(顺便说一句,您应该!):
int cents = company.getAnnouncements().get(0).cents;
我假设 int
是 cents
的类型,您必须向我们提供 Announcement
看起来像什么才能确定,您将如何访问它。
更新
编辑后,您似乎不想 getAnnouncements
,而是 getDividends
。这看起来有点像这样:
double cents = company.getDividends().get(0).getCents();
您需要知道值对象的类型才能知道如何访问它。
如果它是另一个数组列表,你会这样做:
int cents = company.getAnnouncements().get(0).get(1);
我有一个数组列表,其中包含一定数量的元素,每个元素包含两个值(天,美分)
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;
}
}
这是我的公告class,股息延长
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" ;
}
}`
和公司Class
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;
}
}
我相信 company.getAnnouncements()
returns 你是 list
,然后 company.getAnnouncements().get(0)
你会到达 bean
对象。
bean
class 中必须有 getCents()
或其他内容,您的最终代码应类似于 company.getAnnouncements().get(0).getCents()
或者如果 cents
是 bean class 中的一个属性,您也可以 company.getAnnouncements().get(0).cent
就像访问任何其他 getter 一样访问它(这是假设,Announcement
有一个 getCents
方法):
int cents = company.getAnnouncements().get(0).getCents();
或者,如果您没有 getCents
方法(顺便说一句,您应该!):
int cents = company.getAnnouncements().get(0).cents;
我假设 int
是 cents
的类型,您必须向我们提供 Announcement
看起来像什么才能确定,您将如何访问它。
更新
编辑后,您似乎不想 getAnnouncements
,而是 getDividends
。这看起来有点像这样:
double cents = company.getDividends().get(0).getCents();
您需要知道值对象的类型才能知道如何访问它。
如果它是另一个数组列表,你会这样做:
int cents = company.getAnnouncements().get(0).get(1);