我正在学习java并尝试了一些东西。这就是我想要做的。
我正在尝试创建一个对象数组的表。 例如我正在创建一个名为animal的对象,通过它我可以根据用户的选择添加任意数量的动物及其品种。
package tt;
import java.util.Scanner;
public class animal {
String aname;
String abreed;
public animal() {
mainprog aa = new mainprog();
System.out.printf("eneter name of your %s..\n", aa.Animalcat);
Scanner name = new Scanner(System.in);
aname = name.nextLine();
System.out.printf("eneter breed of your %s..\n", aa.Animalcat);
Scanner breed = new Scanner(System.in);
abreed = breed.nextLine();
}
public String getbreed() {
return abreed;
}
public String getname() {
return aname;
}
}
所以主程序询问我想添加多少动物。
package tt;
import java.util.Scanner;
public class mainprog {
public static String Animalcat;
public static void main(String[] args) {
System.out.println("How many animals you want to add..");
Scanner an = new Scanner(System.in);
int animalNumbers = an.nextInt();
animal[][] addAnimal = new animal[animalNumbers][1];
animaltype[] at = new animaltype[animalNumbers];
for (int i = 0; i < animalNumbers; i++) {
at[i] = new animaltype();
Animalcat = at[i].getAnimalType();
for (int j = 0; j < 1; j++) {
addAnimal[i][j] = new animal();
}
}
Display(addAnimal, at);
}
public static void Display(animal x[][], animaltype y[]) {
System.out.println("Your animals are..");
for (int m = 0; m < x.length; m++) {
System.out.printf("Following are the name and the breed of %s ",
y[m].getAnimalType());
System.out.println();
for (int n = 0; n < x[m].length; n++) {
System.out.printf(" %s", x[m][n].aname);
System.out.printf(" %s", x[m][n].abreed);
System.out.println();
}
}
}
}
package tt;
import java.util.Scanner;
public class animaltype {
String animalType;
public animaltype() {
System.out.println("What kind of animal you want to add..");
Scanner at = new Scanner(System.in);
animalType = at.nextLine();
}
public String getAnimalType(){
return animalType;
}
}
我在这里遇到的问题是我可以询问我想要添加多少种动物,但是我无法控制我想添加多少种这类动物。 添加动物时,我只能通过声明[1]手动添加1个动物,并且可以通过用户输入来添加。
addAnimal = new animal[animalNumbers][1];
如果我可以通过声明animal[][] addAnimal = null;
然后稍后用以下内容初始化它来解决这个问题:
animal[][] addAnimal = new animal[animalNumbers][animaltypenumbers];
但我一直得到NullPointerException
。无论如何我可以做到这一点吗?
答案 0 :(得分:1)
创建数组时,它将由其包含的元素的默认值填充。由于Animal
是Object
,因此它将由null
值填充,并且您不能使用任何具有null
值的变量。由于您只是在当前代码中填写animal[i][0]
,因此您不会遇到任何问题。但是当您尝试访问animal[i][1]
时会出现。这种情况发生在Display
方法:
public static void Display(animal x[][], animaltype y[]) {
System.out.println("Your animals are..");
for (int m = 0; m < x.length; m++) {
System.out.printf("Following are the name and the breed of %s ",
y[m].getAnimalType());
System.out.println();
for (int n = 0; n < x[m].length; n++) {
//you only filled elements in x[m][0]
//x[m][n] when n > 0 is null
//so you will get NullPointerException
System.out.printf(" %s", x[m][n].aname);
System.out.printf(" %s", x[m][n].abreed);
System.out.println();
}
}
}
更好的选择:
使用Animal[] addAnimal
代替,您不需要它将其作为数组数组:
public static void main(String[] args) {
System.out.println("How many animals you want to add..");
Scanner an = new Scanner(System.in);
int animalNumbers = an.nextInt();
animal[] addAnimal = new animal[animalNumbers];
animaltype[] at = new animaltype[animalNumbers];
for (int i = 0; i < animalNumbers; i++) {
//at[i] = new animaltype();
//Animalcat = at[i].getAnimalType();
Animalcat = new animaltype();
//for (int j = 0; j < 1; j++) {
// addAnimal[i][j] = new animal();
//}
addAnimal[i] = Animalcat;
}
Display(addAnimal, at);
}
//modify Display method accordingly
更好的选择:
使用List<Animal>
支持的ArrayList<Animal>
代替Animal[]
。 List
让您处理动态增长的元素列表。
public static void main(String[] args) {
System.out.println("How many animals you want to add..");
Scanner an = new Scanner(System.in);
int animalNumbers = an.nextInt();
List<Animal> animals = new ArrayList<Animal>();
for (int i = 0; i < animalNumbers; i++) {
Animalcat = new animaltype();
animals.add(Animalcat);
}
Display(animals);
}
//modify Display method accordingly