新手 :java 如何在一个class输出一个arraylist 求助大侠
importjava.util.ArrayList;publicclassKeeper{privateintid;privateStringname;privateArr...
import java.util.ArrayList;
public class Keeper {
private int id;
private String name;
private ArrayList<String> mSpeciesExpertise = new ArrayList<String>();
private ArrayList<Animals> mAnimalsDuties = new ArrayList<Animals>();
public Keeper(int id,String name)
{
this.id=id;
this.name=name;
//weigh or feed
}
public Keeper(){
name=null;id=0;
}
public void newKeeper(int id,String name)
{
this.id=id;
this.name=name;
}
public int getId(){return id;}
public String getName(){return name;}
public void setId(int a){id=a;}
public void setName(String b){name=b;}
public void addExpertise (String aSpecies) {
if (!mSpeciesExpertise.contains(aSpecies)) {
mSpeciesExpertise.add(aSpecies);}}
/**
* Add a Species to the list of species that this keeper is an expert in
* @param aSpecies
*/
public void addAnimalDuty(Animals anAnimal) {
if (!mAnimalsDuties.contains(anAnimal)) {
mAnimalsDuties.add(anAnimal);
}
}/**
* Add the given animal to the list of animals that this keeper has duties for.
* @param anAnimal
*/
public static void main (String []args){
Keeper k1=new Keeper(1,"Mike");
k1.addExpertise("leo");
Animals an=new Animals("Panda",100,1000,"Panda");
k1.addAnimalDuty(an);
System.out.println(k1.getId()+" "+k1.getName()+k1.mSpeciesExpertise+
k1.mAnimalsDuties
);//k1.mAnimalsDuties 这个arraylist 输不出来, 这个是animal 的arraylist,animal class里面有各种variable的getters
}}
要怎样才能输出k1.mAnimalsDuties里面的东西呢,就是"Panda",100,1000,"Panda"。。。
还有,
我想compose两个class, 一个是animal 一个是keeper, 要怎么做? 展开
public class Keeper {
private int id;
private String name;
private ArrayList<String> mSpeciesExpertise = new ArrayList<String>();
private ArrayList<Animals> mAnimalsDuties = new ArrayList<Animals>();
public Keeper(int id,String name)
{
this.id=id;
this.name=name;
//weigh or feed
}
public Keeper(){
name=null;id=0;
}
public void newKeeper(int id,String name)
{
this.id=id;
this.name=name;
}
public int getId(){return id;}
public String getName(){return name;}
public void setId(int a){id=a;}
public void setName(String b){name=b;}
public void addExpertise (String aSpecies) {
if (!mSpeciesExpertise.contains(aSpecies)) {
mSpeciesExpertise.add(aSpecies);}}
/**
* Add a Species to the list of species that this keeper is an expert in
* @param aSpecies
*/
public void addAnimalDuty(Animals anAnimal) {
if (!mAnimalsDuties.contains(anAnimal)) {
mAnimalsDuties.add(anAnimal);
}
}/**
* Add the given animal to the list of animals that this keeper has duties for.
* @param anAnimal
*/
public static void main (String []args){
Keeper k1=new Keeper(1,"Mike");
k1.addExpertise("leo");
Animals an=new Animals("Panda",100,1000,"Panda");
k1.addAnimalDuty(an);
System.out.println(k1.getId()+" "+k1.getName()+k1.mSpeciesExpertise+
k1.mAnimalsDuties
);//k1.mAnimalsDuties 这个arraylist 输不出来, 这个是animal 的arraylist,animal class里面有各种variable的getters
}}
要怎样才能输出k1.mAnimalsDuties里面的东西呢,就是"Panda",100,1000,"Panda"。。。
还有,
我想compose两个class, 一个是animal 一个是keeper, 要怎么做? 展开
2个回答
展开全部
List类里放的是对象,也就是Animal对象,你可以写一个循环或和迭代器迭代进行输入。如下:
Animal animal = null ;
for (int i = 0; i < mAnimalsDuties.size(); i++) {
//因为List里取出的对象是Object类型,所以要下溯造型成具体类型,即Animal类
animal = (Animal) mAnimalsDuties.get(i) ;
System.our.println("the name:" + animal.getMethod()) ;
//同上........
}
//下面是用迭代器迭代的方法,我还是附上关键代码:
Interator i = mAnimalsDuties.interator() ;
Animal a = null ;
while (i.hasNext()) {
a = i.next() ;
System.our.println("the name:" + animal.getMethod()) ;
//同上........
}
你要Compose2个类是什么意思?是写两个类吗?可以放在同一个文件里,也可以放在不同的文件里,放在一个文件里就只能有一个是public的,随你意啊。
Animal animal = null ;
for (int i = 0; i < mAnimalsDuties.size(); i++) {
//因为List里取出的对象是Object类型,所以要下溯造型成具体类型,即Animal类
animal = (Animal) mAnimalsDuties.get(i) ;
System.our.println("the name:" + animal.getMethod()) ;
//同上........
}
//下面是用迭代器迭代的方法,我还是附上关键代码:
Interator i = mAnimalsDuties.interator() ;
Animal a = null ;
while (i.hasNext()) {
a = i.next() ;
System.our.println("the name:" + animal.getMethod()) ;
//同上........
}
你要Compose2个类是什么意思?是写两个类吗?可以放在同一个文件里,也可以放在不同的文件里,放在一个文件里就只能有一个是public的,随你意啊。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询