我正在学习Java,可以在这里使用一些帮助。
目前我正在进行多态,所以我的代码有一个Dog类,另一个类有一个方法可以将Dog对象添加到列表中(有点像ArrayList)。
package chap8;
/**
* Created by user1 on 7/11/15.
*/
class Dog {
private String name;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}
public class MyDogList {
//array to hold dog objects
private Dog[] dogs = new Dog[5];
//int to check index in dog array
private int nextIndex = 0;
//add method to add dog objects to dog array
private void add(Dog d) {
if(nextIndex < dogs.length) {
dogs[nextIndex] = d;
nextIndex++;
System.out.println("Dog added at position: " + nextIndex);
System.out.println("Space left for " + (dogs.length - nextIndex) + " more dogs.");
}
else {
System.out.println("No more space to add dogs!");
}
}
private void display() {
for (int i = 0; i < dogs.length; i++) {
System.out.println("Name of Dog: " + dogs[i].getName());
}
}
public static void main(String[] args) {
Dog obj1 = new Dog();
obj1.setName("Corgi");
obj1.setAge(2);
MyDogList dogList = new MyDogList();
dogList.add(obj1);
dogList.display();
Dog obj2 = new Dog();
obj2.setName("Labrador");
obj2.setAge(3);
dogList.add(obj2);
Dog obj3 = new Dog();
obj3.setName("Golden Retriever");
obj3.setAge(5);
dogList.add(obj3);
}
}
虽然添加Dog对象的过程有效,但我想要的是另一种方法display
,可能在class MyDogList
中,它会显示dogs
数组中所有Dog对象的名称。 MyDogList
课程。但后来我意识到,如果该方法只接受一个Dog数组,那么就无法知道这个数组中各个dog对象的名称。
我该如何构建这样的方法?
修改
我从评论和答案中意识到,我可以使用getter
获取Dog变量name
来获取名称。但是当我修改代码时(上面用粗体显示),我得到一个Null Pointer Exception
。什么地方出了错?在设置为某个位置之前,我是否尝试访问数组位置?
/usr/lib/jvm/java-8-oracle/bin/java -Didea.launcher.port=7535 -Didea.launcher.bin.path=/home/user1/Documents/idea-IC-141.177.4/bin -Dfile.encoding=UTF-8 -classpath "/home/user1/Dropbox/Development/Java/Head First/bin:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/home/user1/Documents/idea-IC-141.177.4/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain chap8.MyDogList
Exception in thread "main" Dog added at position: 1
Space left for 4 more dogs.
Name of Dog: Corgi
java.lang.NullPointerException
at chap8.MyDogList.display(MyDogList.java:52)
at chap8.MyDogList.main(MyDogList.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 1
答案 0 :(得分:4)
这样的事可能吗?
public void diplay(){
for (Dog dog : dogs){
if (dog != null && dog.getName() != null){
System.out.println("Name of Dog: " + dog.getName());
}
}
}
你的DogList类已经包含了狗的数组。无需将狗数组传递给显示方法。
另请注意检查null,因为您无法确定所有数组位置是否实际已填充。
答案 1 :(得分:-1)
据我所知,你在MyDogList类中需要这样的东西:
public void display(){
for (int i = 0; i < dogs.length; i++) {
System.out.println("Name of Dog: " + dogs[i].getName());
}
}
答案 2 :(得分:-1)
我认为这就是你要找的东西。
public void display() {
for(int i=0; i<nextIndex; i++) {
if(null != this.dogs[i]) {
System.out.println(this.dogs[i].getName());
}
}
迭代到最后添加的对象并在Dog对象中打印名称。你不需要遍历所有数组,你只需要设置nextIndex就可以了。
答案 3 :(得分:-2)
<强>解。强>
添加了显示方法。执行null检查,否则返回nullpointer 异常将发生。
private void display() {
for (Dog d : dogs) {
// null check
if (d != null) {
System.out.println("DogName:" + d.getName());
}
}
}