简体   繁体   English

Java-如何在不同的类中调用add()方法

[英]Java - how to call an add() method in a different class

This is for homework, and I am becoming a little frustrated with how I can't figure out something so simple. 这是为了做作业,我对如何弄清这么简单的东西感到沮丧。

To simplify my code, I have 3 files right now: one class with an add() method I created among other things, one file that tests it (made by the prof), and one that creates the object (which I won't post, b/c its working). 为了简化我的代码,我现在有3个文件:一个我创建了带有add()方法的类,另一个是对其进行测试的文件(由教授制作),另一个创建了对象(我不会)发布,b / c其工作)。 Here's the add() function. 这是add()函数。

EDIT 2: I'm going to add the method that prints the array, maybe that's the problem? 编辑2:我要添加打印数组的方法,也许这是问题吗?

    public class Population {
      private Person[] pop = new Person[15];
      private int numPop = 0;    

      public void add(Person c){ // this object is created in another class, it works fine
        for(int i = 0; i < pop.length; i++){
          if(pop[i] == null) {
            pop[i] = c;
            numPop++;
          } else {}
        }

 public String listPeople(){
      System.out.println("Population with "+numPeople+" people as follows:");
      int i = 0;
      while (i<numPeople){
       System.out.println("A "+pop[i].getAge()+"year old person named "+pop[i].getName());
        i++;
//FYI the get methods are working fine and are in another file.
 } 
      return("");
      }

Then, I run the program in a test file to ensure it works, which was provided to us. 然后,我在一个测试文件中运行该程序以确保其正常工作,该文件已提供给我们。 Here's the part that isn't working 这是无效的部分

public class PopTestProgram{ // FYI the prof created this, I can't change this
  public static void main(String[] args){

    Population pop = new Population(15);

    pop.add(new Person(4, "Bob"));
    pop.add(new Person(25, "Kim"));
    // then adds 8 more people with different ages and names
    // then prints the people

It compiles, but when I run it, it just puts 10 of the last person into the array, then crashes saying there is a problem with the "pop[i] = c;" 它可以编译,但是当我运行它时,它只是将最后一个人的10个放入数组中,然后崩溃说"pop[i] = c;"有问题"pop[i] = c;" line. 线。 I simply cannot figure out what I need to change here. 我根本无法弄清楚我需要在这里进行哪些更改。

I haven't received an email from the prof directly, so I thought I'd ask here. 我没有直接收到教授的电子邮件,所以我想在这里问。

Edit: Here's what it shows after printing out the last person 10 times. 编辑:这是将最后一个人打印10次后显示的内容。 It is showing problems with other methods that I haven't completed yet though... 它显示出我尚未完成的其他方法的问题...

java.lang.ArrayIndexOutOfBoundsException: -1
    at Population.removePerson(Population.java:49)
    at PopTestProgram.main(PopTestProgram.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

In add(Person), you are not stopping when you add an item, so the first item you added is put in all cells in the array, then the rest won't go in at all, since there are no more null cells in array. 在add(Person)中,添加项时不会停止,因此添加的第一个项将放入数组的所有单元格中,因此其余项将根本不会进入,因为在其中不再有空单元格数组。 Break the loop when you find an empty location. 找到空位置时,请中断循环。

public void add(Person c) {
    for(int i = 0; i < pop.length; i++){
      if(pop[i] == null) {
        pop[i] = c;
        numPop++;
        break;
      }
      else {
        //.....
      }
   }
}

Could also just use numPop as the next location in the list, like: 也可以只将numPop用作列表中的下一个位置,例如:

public void add(Person c) {
    if (numPop < pop.length) {
       pop[numPop++] = c;
    }
}

The exception is coming in at Population.removePerson(Population.java:49) , which is not related to add method. 例外情况出现在Population.removePerson(Population.java:49) ,它与add方法无关。 So I assume removePerson is the method to print the person. 因此,我假设removePerson是打印人的方法。 While removing you are calling one extra For loop , make sure your iteration is 10 times only. 删除时,您要调用一个额外的For循环 ,请确保您的迭代仅10次。

The java.lang.ArrayIndexOutOfBoundsException: -1 clearly tells the removePerson method is calling the index -1 also (which doesnt exist causing ArrayIndexOufofBoundsException). java.lang.ArrayIndexOutOfBoundsException: -1清楚地告诉removePerson方法也正在调用索引-1 (不存在导致ArrayIndexOufofBoundsException)。 The removePerson method should start from index 9 to index 0 (or viceversa) [10 iteration in total] and then stop out. removePerson方法应从索引9开始到索引0(反之亦然)[总共进行10次迭代],然后停止。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM