【问题标题】:adding objects to an empty array in Java在Java中将对象添加到空数组
【发布时间】:2013-08-03 23:49:29
【问题描述】:

我一直在做一个关于类继承和抽象类的练习。我对这两个概念很有信心,但正如标题所示,我(仍然)无法将对象添加到类型类数组中。

问题如下:主要有3种文件,

  1. Zoo 类文件(包含 main 方法)
  2. 抽象类动物文件
  3. 以及任何数量的特定动物(牛、马等),它们是 你猜对了,类动物。

类动物园

public class Zoo 
{

private int actual_num_animals;
private int num_cages;
private Animal[] animals;


Zoo()
{
    actual_num_animals = 0;
    num_cages = 20;
}

Zoo(int num_cages)
{
    this.num_cages = num_cages;
}

// adds an animal to Zoo
public void add(Animal a)
{
    for(int i = 0; i < num_cages; i++)
    {
        if(animals[i] != null && animals[i].equals(a) == true)
        {
            System.out.println(a.getName() + " is already in a cage!");
            break;
        }
        else if(animals[i] == null)
        {
            animals[i] = a;
            actual_num_animals++;
            break;
        }
    }

}



// returns the total weight of all animals in zoo
public double total_weight()
{
    double sum = 0;

    for(int i = 0; i < actual_num_animals; i++)
    {
        sum += animals[i].getWeight();
    }
    return sum;
}

//Print out the noises made by all of the animals.
//In otherwords, it calls the makeNoise() method 
//for all animals in the zoo.
public void make_all_noises()
{
    for(int i = 0; i < actual_num_animals; i++)
    {
        animals[i].makeNoise();
        System.out.print("! ");
    }
}

//prints the results of calling toString() on all animals in the zoo.
public void print_all_animals()
{
    for(int i = 0; i < actual_num_animals; i++)
    {
        animals[i].toString();
        System.out.print(" ");
    }
}

 public static void main(String[] args)
    {
        Zoo z = new Zoo();
        Snake sly = new Snake("Sly", 5.0 , 2, 2);
        Snake sly2 = new Snake("Slyme", 10.0 , 1, 2);
        Cow blossy = new Cow("Blossy", 900., 5,  10);
        Horse prince = new Horse("Prince", 1000., 5, 23.2);

        // Following not allowed because Animal is abstract
        //Animal spot = new Animal("Spot", 10., 4);

        z.add(sly);
        z.add(sly2);
        z.add(blossy);
        z.add(prince);

        z.make_all_noises();
        System.out.println("Total weight =" + z.total_weight());
        System.out.println("**************************");
        System.out.println("Animal Printout:");
        z.print_all_animals();  

    }
}

我的问题在于这里的 add 方法。我在第一个 if 语句中不断收到空指针异常

if(animals[i] != null && animals[i].equals(a) == true)

以及第一次在 main 方法中调用此 add 方法。清楚地, 这个条件有问题,并且可能是伴随它的 else-if 条件。

对于我的生活,我无法理解,它不起作用。更糟糕的是,我在之前的练习中遇到了类似的问题:

Class Inheritance in Java

为类 Zoo 编写的 if 和 else-if 条件与上一个问题中概述的 add 函数中的格式完全相同,这是最令人费解的一点。 你们有什么想法吗?

最后,作为参考,虽然我怀疑你会需要它,但我将在下面包含动物类文件和派生类牛文件:

抽象类动物

public abstract class Animal
{
private String name;
private double weight;
private int age;

Animal()
{
    name = "noName";
    weight = 0;
    age = 0;
}

Animal(String n, double weight, int age)
{
    name = n;
    this.weight = weight;
    this.age = age;
}

abstract String makeNoise();

String getName()
{
    return name;
}

double getWeight()
{
    return weight;
}

int getAge()
{
    return age;
}

public String toString()
{
    return name + ", weight: " + weight + "age: " + age;
}

}

派生类牛

public class Cow extends Animal 
{
private int num_spots;

Cow()
{
    super();
    num_spots = 0;
}
Cow(String name, double weight, int age, int num_spots)
{
    super(name, weight, age);
    this.num_spots = num_spots;
}

String makeNoise() 
{
    return "Moooo";
}

public String toString()
{
    return getName() + ", weight: " + getWeight() + "age: " + getAge() +
            "num spots: " + num_spots;
}
}

【问题讨论】:

标签: java arrays class null nullpointerexception


【解决方案1】:

在 Zoo 构造函数中添加一行来初始化数组:

Zoo()
{
    actual_num_animals = 0;
    num_cages = 20;
    animals = new Animal[num_cages];
}

您可能还想实现equals()"Bloch way" 是一个很好的实现)。

【讨论】:

    【解决方案2】:

    在我看来你从来没有初始化过

    private Animal[] animals;
    

    你应该这样做

    animals = new Animal[tot];
    

    其中tot 是您要放入其中的动物总数。请注意,您可能需要 ArrayList 而不是数组,例如为了避免给出起始维度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-03
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 2014-07-16
      • 2016-11-10
      • 1970-01-01
      相关资源
      最近更新 更多