我试图找出Java中的继承和数组,我试图让这些类一起工作。我相信我有继承权,但我仍在努力与数组部分。
有三个文件:1。Person.java -base类2. Student.java-Person.java的派生类3. Family.java-不太确定,我认为它是自己的基类
Person.java有两个实例变量,String name和int age,以及各种构造函数,toString,equals和set / get方法
Student.java,同样是Person的派生类,根据定义,它将包含Person中包含的所有内容,以及另外两个实例变量,String major和double gpa。这个类还有major和gpa的get / set方法,一个equals方法,比较一个班级学生和另一个班级学生,我相信它被称为toString的重叠方法,返回name,age,major和gpa所有在一个字符串。
最后,Family.java是main方法所在的位置。它创建了一个Person类型的数组,添加" Persons"到这个数组,然后输出它们。
我收到的错误是:" main" java.lang.ArrayIndexOutOfBoundsException:8
我无法弄清楚为什么这个程序不能正常工作,并希望有任何帮助来解决这个问题。谢谢。
Person.java类
public class Person
{
private String name;
private int age;
public Person()
{
name = "John Smith";
age = 1;
}
public Person(String n, int a)
{
name = n;
age = a;
}
public String toString()
{
return ("Name: " + getName() + ", Age: " + age + " ");
}
public boolean equals(Person otherPerson)
{
return (getName().equals(otherPerson.getName()) && (age == otherPerson.age));
}
public String getName()
{
return name;
}
public void setName(String newName)
{
name = newName;
}
public int getAge()
{
return age;
}
public void setAge(int newAge)
{
age = newAge;
}
}
Student.java类
public class Student extends Person
{
private String major;
private double gpa;
public Student()
{
super();
major = "Undecided";
gpa = 0.0;
}
public Student(String theName, int theAge, String theMajor, double theGpa)
{
super(theName, theAge);
setMajor(theMajor);
setGpa(theGpa);
}
public String toString()
{
return ("Name: " + getName() + ", Age: " + getAge() + ", Major: " + major + ", GPA: " + gpa);
}
public boolean equals(Student otherStudent)
{
return (major.equals(otherStudent.major) && (gpa == otherStudent.gpa));
}
public String getMajor()
{
return major;
}
public void setMajor(String newMajor)
{
major = newMajor;
}
public double getGpa()
{
return gpa;
}
public void setGpa(double newGpa)
{
gpa = newGpa;
}
}
Family.java类
public class Family
{
private int famArray = 0;
private Person[] family;
public Family(int size_of_family)
{
famArray = size_of_family;
family = new Person[famArray];
}
public void addPerson(Person p)
{
boolean isPresent = false;
int i;
for(i = 0; i < family.length; i++)
{
if(family[i] != null && family[i].equals(p))
{
isPresent = true;
System.out.println(p.getName() +
" is already present in the family");
}
}
if(isPresent == false)
family[i] = p;
}
public void printOutFamily()
{
for(int i = 0; i < family.length; i++)
{
System.out.println(family[i].toString());
}
}
public static void main(String[] args)
{
Family f = new Family(8);
Person fred= new Person("Fred Flintstone", 50);
System.out.println("created " + fred);
f.addPerson(fred);
f.addPerson(fred);
Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1);
System.out.println("created "+ fredStudent);
f.addPerson(fredStudent);
Person wilma = new Person("Wilma Flintstone", 48);
f.addPerson(wilma);
Student george= new Student("George", 21, "Politics", 3.1);
System.out.println("created " + george);
f.addPerson(george);
george.setName("Georgie");
f.addPerson(new Student("George", 21, "Politics", 3.1));
f.addPerson(new Student("John", 18, "Geology", 2.9));
f.addPerson(new Student("Jane", 21, "Music", 3.2));
f.addPerson(new Student("Tarzan", 22, "Gymnastics", 4.0));
f.addPerson(new Student("Jim", 21, "Physics", 2.5));
System.out.println("****** family listing: ");
f.printOutFamily();
}
}
答案 0 :(得分:2)
问题在于Family#addPerson
方法:
if(isPresent == false)
family[i] = p;
您正在添加位置i
中的元素。如果找不到该元素,则i
值将为family.length
,从而为您提供例外。
改为使用int famArray
字段:
if(isPresent == false) {
family[famArray++] = p;
}
或者以更简单的方式为初学者提供:
if(isPresent == false) {
family[famArray] = p;
famArray = famArray + 1;
}
作为当前问题的补充,您应首先检查famArray
元素是否等于family.length
。如果它们相同,则增加数组或不允许更多元素。
你说你有同样的问题。这是因为您使用famArray
类构造函数中指出的数组长度初始化Family
:
public Family(int size_of_family) {
famArray = size_of_family; //here
family = new Person[famArray];
}
将代码更改为:
public Family(int size_of_family) {
famArray = 0;
family = new Person[size_of_family];
}
你已经完成了。
答案 1 :(得分:2)
问题在于
行family[i] = p;
这发生在for
循环之后,它使i
增加到等于数组的长度。我没有任何修改建议,因为我不确定你在这里做什么。