问题在于我试图将一个类添加到一个数组中,但它只给了我一个空值。

huangapple go评论118阅读模式
英文:

problem where I try to add a class to an array and it only give me null

问题

import java.util.Random;

public class Start {
    public static void main(String[] args) {
        Village village = new Village();
    }
}

class Village {
    final int SIZE = 1000;
    Person[] population = new Person[SIZE];
    
    Village() {
        for (int i = 0; i < SIZE; i = i + 1) {
            Person person = new Person();
            population[i] = person;
        }
    }
    
    int countSick() {
        int sickCount = 0;
        for (Person person : population) {
            boolean checker = person.isSick;
            if (checker == true) {
                sickCount = sickCount + 1;
            }
        }
        return sickCount;
    }
}

class Person {
    boolean isSick;
    final double INIT_SICK_PROB = 0.32;
    
    Person() {
        Random rand = new Random();
        double checker = rand.nextDouble();
        if (checker <= INIT_SICK_PROB) {
            isSick = true;
        } else {
            isSick = false;
        }
    }
}
英文:

I have an assignment where I am supposed to use classes to build a village population in an array and where every instance in the array is a special class which at the moment only contains a boolean value determining if they are sick or not but it only adds null to the array.

import java.lang.reflect.Array;
import java.util.Random;
public class Start {
    public static void main(String[] args) {
        Village village = new Village();

    }
}

class Village {
    final int SIZE = 1000;
    Person[] population = new Person[SIZE];
    Village() {
        for (int i = 0; i &lt; SIZE; i = i + 1) {
            Person personen = new Person();
            Array.set(population, i, personen);
        }
    }
    static int countSick; {
        int sjuka = 0;
        for (Person personen: population) {
            boolean checker = personen.isSick;
            if (checker == true) {
                sjuka = sjuka + 1;
            }
        }
    }

}
class Person {
    boolean isSick;
    final double INIT_SICK_PROB = 0.32;
    Person() {
        Random rand = new Random();
        double checker = rand.nextDouble();
        if (checker &lt;= INIT_SICK_PROB) {
            isSick = true;
        } else {
            isSick = false;
        }

    }
}

答案1

得分: 1

看看你的类 Village

class Village {
    final int SIZE = 1000;
    Person[] population = new Person[SIZE];
    Village() {
        for (int i = 0; i < SIZE; i = i + 1) {
            Person personen = new Person();
            Array.set(population, i, personen);
        }
    }
    public int countSick() {
        int sjuka = 0;
        for (Person personen : population) {
            boolean checker = personen.isSick;
            if (checker == true) {
                sjuka = sjuka + 1;
            }
        }
        return sjuka;
    }
}

这些行 static int countSick; {...} 创建了一个静态变量 countSick,后面跟着一个代码块。

这个代码块是一个 初始化块,会在 构造函数之前 运行。这意味着:

for (Person personen : population) {

会在它被构造函数填充之前被访问,因此会抛出 NullPointerException

你可能想把 countSick 做成一个 方法。为此,你需要将那一行改成:

public int countSick() {

就是方法定义的样子。另外,由于你可能想要从 Village 实例中访问 population 数组,你需要移除 static 修饰符。

另外,不要忘记在方法末尾 returnsjuka,否则会得到编译错误。

英文:

Look at your class Village:

class Village {
    final int SIZE = 1000;
    Person[] population = new Person[SIZE];
    Village() {
        for (int i = 0; i &lt; SIZE; i = i + 1) {
            Person personen = new Person();
            Array.set(population, i, personen);
        }
    }
    static int countSick; {
        int sjuka = 0;
        for (Person personen: population) {
        boolean checker = personen.isSick;
            if (checker == true) {
                sjuka = sjuka + 1;
            }
        }
    }

}

These lines static int countSick; {...} create a static variable countSick followed by a code block.

This code block is an initializer block and will be run before the constructor. That means,

for (Person personen: population) {

will be accessed before it is populated by the constructor and therefore throws a NullPointerException.

You probably wanted to make countSick a method. For that you need to change the line to

public int countSick() { 

instead. That's how a method definition looks. Also, since you probably want to access the population array from a Village instance, you need to remove the static modifier.

Also, don't forget to return the value sjuka at the end of the method, otherwise you'll get a compiler error.

答案2

得分: 0

你的main方法目前只是创建了一个新的村庄。它的数组应该在构造函数中被初始化和填充,因为这是在构造函数中进行的。但是这些值在初始化之后从未被访问过。

字段"countSick"从未被访问过,并且在Village中被声明为静态字段,因此是与类相关联的,而"population"字段是与对象相关联的。我认为这可能是一个返回计数的方法?

总的来说,你的架构存在缺陷,我猜这可能是由于对面向对象和Java语言的知识缺失所导致的。

  • 首先初始化村庄
  • 然后用人填充它的数组
  • 然后通过村庄实例访问数组,对患病的人进行检查

这意味着你需要两个实体类:Village和Person。一个可以处理村庄数组以读取患病人数的方法。以及声明处理过程开始的Main类。

英文:

Your main-Method currently only creates a new Village. Its array should be initialized and filled since this happens in its constructor. But the values are never accessed after the initialization.

The field "countSick" is never accessed and is also declared static inside Village an therefore class bound, while the field population is object bound. i believe this is meant to be a method that returns the count?

Your architecture has flaws in general which i would guess result from missing knowledge about object orientation and the Java language?

  • First initialize the village
  • Then fill its array with people
  • Then access the array through the instance of the village to run your check on sick people

This means you need 2 Entity-Class Village and Person. A method that can process the villages array to read the sick count. And your Main-Class that declares the start of your process.

huangapple
  • 本文由 发表于 2020年10月27日 01:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64541887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定