【发布时间】:2020-07-11 05:35:34
【问题描述】:
我试图理解为什么我不能打印这种方法。它没有告诉我我的代码有任何问题,所以我无法弄清楚我在这里缺少什么。
package practice;
class Persons{
String name;
int id;
public void speak(String name,int id) {
System.out.println("hello i am " + name + " and my id is " + id);
this.name = name;
this.id = id;
}
}
public class demo1 {
public static void main(String[] args) {
Persons person[] = new Persons[5];
person[0].speak("guy", 1);
}
}
【问题讨论】:
-
您正在创建一个长度为 5 的数组,但是数组中没有元素,但数组中的所有元素都是
null。
标签: java arrays object arraylist